blob: c143041326d2bee3a422988b4a9fd75e4b3e4e18 [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
Songchun Fan01c4e912020-03-03 17:58:20 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
23#include <android-base/strings.h>
24#include <fs_mgr.h>
25#include <fscrypt/fscrypt.h>
26#include <private/android_filesystem_config.h>
27#include <utils/Trace.h>
28
29#include <fstream>
30#include <thread>
31
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060032#include "Benchmark.h"
Daniel Rosenberg4f684712018-08-28 01:58:49 -070033#include "Checkpoint.h"
Eric Biggersa701c452018-10-23 13:06:55 -070034#include "FsCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080035#include "IdleMaint.h"
Seth Moore5a43d612021-01-19 17:51:51 +000036#include "KeyStorage.h"
Martijn Coeneneed957f2020-11-12 10:59:13 +010037#include "Keymaster.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060038#include "MetadataCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080039#include "MoveStorage.h"
40#include "Process.h"
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070041#include "VoldNativeServiceValidation.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080042#include "VoldUtil.h"
43#include "VolumeManager.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070044#include "cryptfs.h"
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -070045#include "incfs.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -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(
Songchun Fan01c4e912020-03-03 17:58:20 -0800147 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
Martijn Coenen23c04452020-04-29 07:49:41 +0200179binder::Status VoldNativeService::abortFuse() {
180 ENFORCE_SYSTEM_OR_ROOT;
181 ACQUIRE_LOCK;
182
183 return translate(VolumeManager::Instance()->abortFuse());
184}
185
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600186binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
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()->onUserAdded(userId, userSerial));
191}
192
193binder::Status VoldNativeService::onUserRemoved(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
197 return translate(VolumeManager::Instance()->onUserRemoved(userId));
198}
199
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700200binder::Status VoldNativeService::onUserStarted(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
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700204 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600205}
206
207binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100208 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600209 ACQUIRE_LOCK;
210
211 return translate(VolumeManager::Instance()->onUserStopped(userId));
212}
213
Sudheer Shankad484aa92018-07-31 10:07:34 -0700214binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700215 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700216 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700217}
218
Sudheer Shankad484aa92018-07-31 10:07:34 -0700219binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700220 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700221 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700222}
223
Jeff Sharkey401b2602017-12-14 22:15:20 -0700224binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100225 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700226 ACQUIRE_LOCK;
227
228 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
229}
230
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600231binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700232 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100233 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600234 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600235 ACQUIRE_LOCK;
236
237 auto disk = VolumeManager::Instance()->findDisk(diskId);
238 if (disk == nullptr) {
239 return error("Failed to find disk " + diskId);
240 }
241 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700242 case PARTITION_TYPE_PUBLIC:
243 return translate(disk->partitionPublic());
244 case PARTITION_TYPE_PRIVATE:
245 return translate(disk->partitionPrivate());
246 case PARTITION_TYPE_MIXED:
247 return translate(disk->partitionMixed(ratio));
248 default:
249 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600250 }
251}
252
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600253binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700254 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100255 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600256 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600257 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600258 ACQUIRE_LOCK;
259
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600260 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600261}
262
Zim5048b4b2019-11-19 09:16:03 +0000263binder::Status VoldNativeService::mount(
264 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
265 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100266 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600267 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600268 ACQUIRE_LOCK;
269
270 auto vol = VolumeManager::Instance()->findVolume(volId);
271 if (vol == nullptr) {
272 return error("Failed to find volume " + volId);
273 }
274
275 vol->setMountFlags(mountFlags);
276 vol->setMountUserId(mountUserId);
277
Zim5048b4b2019-11-19 09:16:03 +0000278 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600279 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000280 vol->setMountCallback(nullptr);
281
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700282 if (res != OK) {
283 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600284 }
Zim3623a212019-07-19 16:46:53 +0100285
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700286 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600287}
288
289binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100290 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600291 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600292 ACQUIRE_LOCK;
293
294 auto vol = VolumeManager::Instance()->findVolume(volId);
295 if (vol == nullptr) {
296 return error("Failed to find volume " + volId);
297 }
298 return translate(vol->unmount());
299}
300
301binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100302 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600303 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600304 ACQUIRE_LOCK;
305
306 auto vol = VolumeManager::Instance()->findVolume(volId);
307 if (vol == nullptr) {
308 return error("Failed to find volume " + volId);
309 }
310 return translate(vol->format(fsType));
311}
312
Jeff Sharkey2048a282017-06-15 09:59:43 -0600313static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600314 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600315 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600316 } else {
317 auto vol = VolumeManager::Instance()->findVolume(volId);
318 if (vol == nullptr) {
319 return error("Failed to find volume " + volId);
320 }
321 if (vol->getType() != VolumeBase::Type::kPrivate) {
322 return error("Volume " + volId + " not private");
323 }
324 if (vol->getState() != VolumeBase::State::kMounted) {
325 return error("Volume " + volId + " not mounted");
326 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600327 *path = vol->getPath();
328 if (path->empty()) {
329 return error("Volume " + volId + " missing path");
330 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600331 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700332 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600333}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600334
Jeff Sharkey2048a282017-06-15 09:59:43 -0600335binder::Status VoldNativeService::benchmark(
Songchun Fan01c4e912020-03-03 17:58:20 -0800336 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100337 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600338 CHECK_ARGUMENT_ID(volId);
339 ACQUIRE_LOCK;
340
341 std::string path;
342 auto status = pathForVolId(volId, &path);
343 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600344
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700345 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700346 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600347}
348
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700349binder::Status VoldNativeService::moveStorage(
Songchun Fan01c4e912020-03-03 17:58:20 -0800350 const std::string& fromVolId, const std::string& toVolId,
351 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100352 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600353 CHECK_ARGUMENT_ID(fromVolId);
354 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600355 ACQUIRE_LOCK;
356
357 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
358 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
359 if (fromVol == nullptr) {
360 return error("Failed to find volume " + fromVolId);
361 } else if (toVol == nullptr) {
362 return error("Failed to find volume " + toVolId);
363 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600364
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700365 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700366 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600367}
368
369binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100370 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600371 ACQUIRE_LOCK;
372
Sudheer Shanka817b9112018-12-13 17:40:28 -0800373 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600374}
375
Ricky Wai6b122572020-02-28 16:30:47 +0000376binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
377 const std::vector<std::string>& packageNames) {
378 ENFORCE_SYSTEM_OR_ROOT;
379 ACQUIRE_LOCK;
380
Ricky Waia2ca11e2021-01-15 14:03:23 +0000381 return translate(VolumeManager::Instance()->handleAppStorageDirs(uid, pid,
382 false /* doUnmount */, packageNames));
383}
384
385binder::Status VoldNativeService::unmountAppStorageDirs(int uid, int pid,
386 const std::vector<std::string>& packageNames) {
387 ENFORCE_SYSTEM_OR_ROOT;
388 ACQUIRE_LOCK;
389
390 return translate(VolumeManager::Instance()->handleAppStorageDirs(uid, pid,
391 true /* doUnmount */, packageNames));
Ricky Wai6b122572020-02-28 16:30:47 +0000392}
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
Ricky Waibbfb6ea2020-12-03 15:32:52 +0000402binder::Status VoldNativeService::ensureAppDirsCreated(const std::vector<std::string>& paths,
403 int32_t appUid) {
404 ENFORCE_SYSTEM_OR_ROOT;
405 ACQUIRE_LOCK;
406
407 return translate(VolumeManager::Instance()->ensureAppDirsCreated(paths, appUid));
408}
409
Martijn Coenen816f4d92020-02-18 15:06:37 +0100410binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
411 ENFORCE_SYSTEM_OR_ROOT;
412 CHECK_ARGUMENT_PATH(path);
413 ACQUIRE_LOCK;
414
415 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
416}
417
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600418binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700419 const std::string& sourceKey, int32_t ownerGid,
420 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100421 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600422 CHECK_ARGUMENT_PATH(sourcePath);
423 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600424 ACQUIRE_LOCK;
425
426 return translate(
Songchun Fan01c4e912020-03-03 17:58:20 -0800427 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600428}
429
430binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100431 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600432 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600433 ACQUIRE_LOCK;
434
435 return translate(VolumeManager::Instance()->destroyObb(volId));
436}
437
Risan82e90de2020-02-04 16:07:21 +0900438binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
439 const std::string& mountPath,
440 const std::string& fsType,
441 const std::string& fsUuid,
442 const std::string& fsLabel, int32_t flags,
443 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100444 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900445 CHECK_ARGUMENT_PATH(sourcePath);
446 CHECK_ARGUMENT_PATH(mountPath);
447 CHECK_ARGUMENT_HEX(fsUuid);
448 // Label limitation seems to be different between fs (including allowed characters), so checking
449 // is quite meaningless.
450 ACQUIRE_LOCK;
451
Risan82e90de2020-02-04 16:07:21 +0900452 return translate(VolumeManager::Instance()->createStubVolume(
453 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900454}
455
456binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100457 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900458 CHECK_ARGUMENT_ID(volId);
459 ACQUIRE_LOCK;
460
461 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
462}
463
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700464binder::Status VoldNativeService::fstrim(
Songchun Fan01c4e912020-03-03 17:58:20 -0800465 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100466 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600467 ACQUIRE_LOCK;
468
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700469 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700470 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600471}
472
Jin Qiana370c142017-10-17 15:41:45 -0700473binder::Status VoldNativeService::runIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800474 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100475 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700476 ACQUIRE_LOCK;
477
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700478 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700479 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700480}
481
482binder::Status VoldNativeService::abortIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800483 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100484 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700485 ACQUIRE_LOCK;
486
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700487 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700488 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700489}
490
Risan8f6198d2018-10-26 20:56:45 -0600491binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700492 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100493 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600494 ACQUIRE_LOCK;
495
Risan8f6198d2018-10-26 20:56:45 -0600496 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600497}
498
Risan8f6198d2018-10-26 20:56:45 -0600499binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100500 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600501 ACQUIRE_LOCK;
502
Risan8f6198d2018-10-26 20:56:45 -0600503 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
504}
505
506binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
507 int32_t flags,
508 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100509 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600510 ACQUIRE_LOCK;
511
512 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
513 if (fd == -1) {
514 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
515 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
516 " flags: " + std::to_string(flags));
517 }
518
519 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700520 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600521}
522
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600523binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100524 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600525 ACQUIRE_CRYPT_LOCK;
526
527 return translate(cryptfs_check_passwd(password.c_str()));
528}
529
530binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100531 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600532 ACQUIRE_CRYPT_LOCK;
533
534 // Spawn as thread so init can issue commands back to vold without
535 // causing deadlock, usually as a result of prep_data_fs.
536 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700537 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600538}
539
540binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100541 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600542 ACQUIRE_CRYPT_LOCK;
543
544 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700545 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600546}
547
548static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700549 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600550 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
551
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600552 for (int tries = 0; tries < 2; ++tries) {
553 int rc;
554 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800555 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600556 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800557 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600558 }
559
560 if (rc == 0) {
561 return 0;
562 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600563 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600564 }
565 }
566
567 return -1;
568}
569
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700570binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
571 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100572 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600573 ACQUIRE_CRYPT_LOCK;
574
Paul Crowley0fd26262018-01-30 09:48:19 -0800575 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700576 if (fscrypt_is_native()) {
577 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
578 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600579 }
Eric Biggersa701c452018-10-23 13:06:55 -0700580 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600581
582 // Spawn as thread so init can issue commands back to vold without
583 // causing deadlock, usually as a result of prep_data_fs.
584 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700585 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600586}
587
588binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700589 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100590 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600591 ACQUIRE_CRYPT_LOCK;
592
593 return translate(cryptfs_changepw(passwordType, password.c_str()));
594}
595
596binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100597 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600598 ACQUIRE_CRYPT_LOCK;
599
600 return translate(cryptfs_verify_passwd(password.c_str()));
601}
602
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700603binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100604 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600605 ACQUIRE_CRYPT_LOCK;
606
607 char buf[PROPERTY_VALUE_MAX];
608 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
609 return error(StringPrintf("Failed to read field %s", key.c_str()));
610 } else {
611 *_aidl_return = buf;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700612 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600613 }
614}
615
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700616binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100617 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600618 ACQUIRE_CRYPT_LOCK;
619
620 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
621}
622
623binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _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 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700628 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600629}
630
631binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100632 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600633 ACQUIRE_CRYPT_LOCK;
634
635 const char* res = cryptfs_get_password();
636 if (res != nullptr) {
637 *_aidl_return = res;
638 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700639 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600640}
641
642binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100643 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600644 ACQUIRE_CRYPT_LOCK;
645
646 cryptfs_clear_password();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700647 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648}
649
650binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100651 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600652 ACQUIRE_CRYPT_LOCK;
653
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700654 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600655}
656
657binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100658 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600659 ACQUIRE_CRYPT_LOCK;
660
Eric Biggersa701c452018-10-23 13:06:55 -0700661 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600662 // Spawn as thread so init can issue commands back to vold without
663 // causing deadlock, usually as a result of prep_data_fs.
664 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600665 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700666 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600667}
668
669binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100670 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600671 ACQUIRE_CRYPT_LOCK;
672
Eric Biggersa701c452018-10-23 13:06:55 -0700673 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600674}
675
676binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100677 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600678 ACQUIRE_CRYPT_LOCK;
679
680 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700681 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600682}
683
Paul Lawrence236e5e82019-06-25 14:44:33 -0700684binder::Status VoldNativeService::mountFstab(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
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800689 return translateBool(
690 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false, "null"));
Paul Crowley0fd26262018-01-30 09:48:19 -0800691}
692
Paul Lawrence236e5e82019-06-25 14:44:33 -0700693binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800694 const std::string& mountPoint, bool shouldFormat,
695 const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100696 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800697 ACQUIRE_LOCK;
698
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800699 return translateBool(
700 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat, fsType));
Paul Crowley0fd26262018-01-30 09:48:19 -0800701}
702
Seth Moore5a43d612021-01-19 17:51:51 +0000703binder::Status VoldNativeService::setStorageBindingSeed(const std::vector<uint8_t>& seed) {
704 ENFORCE_SYSTEM_OR_ROOT;
705 ACQUIRE_CRYPT_LOCK;
706
707 return translateBool(setKeyStorageBindingSeed(seed));
708}
709
Songchun Fan01c4e912020-03-03 17:58:20 -0800710binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
711 bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100712 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600713 ACQUIRE_CRYPT_LOCK;
714
Eric Biggersa701c452018-10-23 13:06:55 -0700715 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600716}
717
718binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100719 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600720 ACQUIRE_CRYPT_LOCK;
721
Eric Biggersa701c452018-10-23 13:06:55 -0700722 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600723}
724
725binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700726 const std::string& token,
727 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100728 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600729 ACQUIRE_CRYPT_LOCK;
730
Eric Biggersa701c452018-10-23 13:06:55 -0700731 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600732}
733
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700734binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
735 const std::string& token,
736 const std::string& secret) {
737 ENFORCE_SYSTEM_OR_ROOT;
738 ACQUIRE_CRYPT_LOCK;
739
740 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
741}
742
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600743binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100744 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600745 ACQUIRE_CRYPT_LOCK;
746
Eric Biggersa701c452018-10-23 13:06:55 -0700747 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600748}
749
750binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700751 const std::string& token,
752 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100753 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600754 ACQUIRE_CRYPT_LOCK;
755
Eric Biggersa701c452018-10-23 13:06:55 -0700756 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600757}
758
759binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100760 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600761 ACQUIRE_CRYPT_LOCK;
762
Eric Biggersa701c452018-10-23 13:06:55 -0700763 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600764}
765
Jooyung Han4fcb7072020-01-23 13:23:26 +0900766binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700767 int32_t userId, int32_t userSerial,
768 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100769 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700770 std::string empty_string = "";
771 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700772 CHECK_ARGUMENT_HEX(uuid_);
773
774 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700775 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600776}
777
Jooyung Han4fcb7072020-01-23 13:23:26 +0900778binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700779 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100780 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700781 std::string empty_string = "";
782 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700783 CHECK_ARGUMENT_HEX(uuid_);
784
785 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700786 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600787}
788
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700789binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
790 int32_t appId, const std::string& sandboxId,
791 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700792 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700793}
794
795binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700796 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700797 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700798 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700799}
800
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700801binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100802 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700803 ACQUIRE_LOCK;
804
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700805 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700806}
807
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700808binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100809 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700810 ACQUIRE_LOCK;
811
812 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700813 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700814}
815
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700816binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100817 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700818 ACQUIRE_LOCK;
819
820 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700821 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700822}
823
David Anderson23850d32020-06-10 23:51:17 -0700824binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
825 ENFORCE_SYSTEM_OR_ROOT;
826 ACQUIRE_LOCK;
827
828 *_aidl_return = cp_isCheckpointing();
829 return Ok();
830}
831
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700832binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100833 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700834 ACQUIRE_LOCK;
835
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700836 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700837}
838
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700839binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100840 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700841 ACQUIRE_LOCK;
842
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700843 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700844}
845
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700846binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100847 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700848 CHECK_ARGUMENT_PATH(mountPoint);
849 ACQUIRE_LOCK;
850
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700851 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700852}
853
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800854binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100855 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800856 CHECK_ARGUMENT_PATH(mountPoint);
857 ACQUIRE_LOCK;
858
859 return cp_restoreCheckpoint(mountPoint, count);
860}
861
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700862binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100863 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700864 ACQUIRE_LOCK;
865
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700866 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700867}
868
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700869binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100870 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700871 ACQUIRE_LOCK;
872
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700873 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700874 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700875}
876
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800877binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100878 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800879 ACQUIRE_LOCK;
880
881 return cp_supportsCheckpoint(*_aidl_return);
882}
883
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700884binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100885 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700886 ACQUIRE_LOCK;
887
888 return cp_supportsBlockCheckpoint(*_aidl_return);
889}
890
891binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100892 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700893 ACQUIRE_LOCK;
894
895 return cp_supportsFileCheckpoint(*_aidl_return);
896}
897
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100898binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100899 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100900 ACQUIRE_LOCK;
901
902 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700903 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100904}
905
Martijn Coeneneed957f2020-11-12 10:59:13 +0100906binder::Status VoldNativeService::earlyBootEnded() {
907 ENFORCE_SYSTEM_OR_ROOT;
908 ACQUIRE_LOCK;
909
910 Keymaster::earlyBootEnded();
911 return Ok();
912}
913
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800914binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700915 ENFORCE_SYSTEM_OR_ROOT;
916
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700917 *_aidl_return = incfs::enabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700918 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800919}
920
921binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800922 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800923 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700924 ENFORCE_SYSTEM_OR_ROOT;
925 CHECK_ARGUMENT_PATH(backingPath);
926 CHECK_ARGUMENT_PATH(targetDir);
927
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700928 auto control = incfs::mount(backingPath, targetDir,
929 {.flags = IncFsMountFlags(flags),
930 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
931 // Mount with read logs disabled.
932 .readLogBufferPages = 0});
933 if (!control) {
934 return translate(-errno);
Songchun Fanab55cec2019-12-02 10:50:12 -0800935 }
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700936 auto fds = control.releaseFds();
937 using android::base::unique_fd;
938 _aidl_return->cmd.reset(unique_fd(fds[CMD].release()));
939 _aidl_return->pendingReads.reset(unique_fd(fds[PENDING_READS].release()));
940 _aidl_return->log.reset(unique_fd(fds[LOGS].release()));
Yurii Zubrytskyi0c0f8392020-12-08 07:33:37 -0800941 if (fds[BLOCKS_WRITTEN].ok()) {
942 _aidl_return->blocksWritten.emplace(unique_fd(fds[BLOCKS_WRITTEN].release()));
943 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700944 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800945}
946
947binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700948 ENFORCE_SYSTEM_OR_ROOT;
949 CHECK_ARGUMENT_PATH(dir);
950
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700951 return translate(incfs::unmount(dir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800952}
953
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700954binder::Status VoldNativeService::setIncFsMountOptions(
955 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
956 bool enableReadLogs) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -0700957 ENFORCE_SYSTEM_OR_ROOT;
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700958
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700959 auto incfsControl =
Yurii Zubrytskyi0c0f8392020-12-08 07:33:37 -0800960 incfs::createControl(control.cmd.get(), control.pendingReads.get(), control.log.get(),
961 control.blocksWritten ? control.blocksWritten->get() : -1);
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700962 auto cleanupFunc = [](auto incfsControl) {
963 for (auto& fd : incfsControl->releaseFds()) {
964 (void)fd.release();
965 }
966 };
967 auto cleanup =
968 std::unique_ptr<incfs::Control, decltype(cleanupFunc)>(&incfsControl, cleanupFunc);
969 if (auto error = incfs::setOptions(
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700970 incfsControl,
971 {.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
972 .readLogBufferPages = enableReadLogs ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES : 0});
973 error < 0) {
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700974 return binder::Status::fromServiceSpecificError(error);
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700975 }
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700976
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700977 return Ok();
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700978}
979
Songchun Fanab55cec2019-12-02 10:50:12 -0800980binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
981 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700982 ENFORCE_SYSTEM_OR_ROOT;
983 CHECK_ARGUMENT_PATH(sourceDir);
984 CHECK_ARGUMENT_PATH(targetDir);
985
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700986 return translate(incfs::bindMount(sourceDir, targetDir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800987}
988
Yo Chiang0af25a32020-10-07 14:20:00 +0800989binder::Status VoldNativeService::destroyDsuMetadataKey(const std::string& dsuSlot) {
990 ENFORCE_SYSTEM_OR_ROOT;
991 ACQUIRE_LOCK;
992
993 return translateBool(destroy_dsu_metadata_key(dsuSlot));
994}
995
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600996} // namespace vold
997} // namespace android