blob: 938e7dbf53db7a43c8e7ddd62dcd578977f208e1 [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;
lijiaziffe76222021-03-17 16:51:16 +0800181 // if acquire lock, maybe lead to a deadlock if lock is held by a
182 // thread that is blocked on a FUSE operation.
183 // abort fuse doesn't need to access any state, so do not acquire lock
Martijn Coenen23c04452020-04-29 07:49:41 +0200184
185 return translate(VolumeManager::Instance()->abortFuse());
186}
187
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600188binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100189 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600190 ACQUIRE_LOCK;
191
192 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
193}
194
195binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100196 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600197 ACQUIRE_LOCK;
198
199 return translate(VolumeManager::Instance()->onUserRemoved(userId));
200}
201
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700202binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100203 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600204 ACQUIRE_LOCK;
205
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700206 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600207}
208
209binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100210 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600211 ACQUIRE_LOCK;
212
213 return translate(VolumeManager::Instance()->onUserStopped(userId));
214}
215
Sudheer Shankad484aa92018-07-31 10:07:34 -0700216binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700217 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700218 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700219}
220
Sudheer Shankad484aa92018-07-31 10:07:34 -0700221binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700222 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700223 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700224}
225
Jeff Sharkey401b2602017-12-14 22:15:20 -0700226binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100227 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700228 ACQUIRE_LOCK;
229
230 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
231}
232
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600233binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700234 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100235 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600236 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600237 ACQUIRE_LOCK;
238
239 auto disk = VolumeManager::Instance()->findDisk(diskId);
240 if (disk == nullptr) {
241 return error("Failed to find disk " + diskId);
242 }
243 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700244 case PARTITION_TYPE_PUBLIC:
245 return translate(disk->partitionPublic());
246 case PARTITION_TYPE_PRIVATE:
247 return translate(disk->partitionPrivate());
248 case PARTITION_TYPE_MIXED:
249 return translate(disk->partitionMixed(ratio));
250 default:
251 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600252 }
253}
254
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600255binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700256 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100257 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600258 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600259 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600260 ACQUIRE_LOCK;
261
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600262 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600263}
264
Zim5048b4b2019-11-19 09:16:03 +0000265binder::Status VoldNativeService::mount(
266 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
267 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100268 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600269 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600270 ACQUIRE_LOCK;
271
272 auto vol = VolumeManager::Instance()->findVolume(volId);
273 if (vol == nullptr) {
274 return error("Failed to find volume " + volId);
275 }
276
277 vol->setMountFlags(mountFlags);
278 vol->setMountUserId(mountUserId);
279
Zim5048b4b2019-11-19 09:16:03 +0000280 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600281 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000282 vol->setMountCallback(nullptr);
283
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700284 if (res != OK) {
285 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600286 }
Zim3623a212019-07-19 16:46:53 +0100287
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700288 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600289}
290
291binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100292 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600293 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600294 ACQUIRE_LOCK;
295
296 auto vol = VolumeManager::Instance()->findVolume(volId);
297 if (vol == nullptr) {
298 return error("Failed to find volume " + volId);
299 }
300 return translate(vol->unmount());
301}
302
303binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100304 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600305 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600306 ACQUIRE_LOCK;
307
308 auto vol = VolumeManager::Instance()->findVolume(volId);
309 if (vol == nullptr) {
310 return error("Failed to find volume " + volId);
311 }
312 return translate(vol->format(fsType));
313}
314
Jeff Sharkey2048a282017-06-15 09:59:43 -0600315static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600316 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600317 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600318 } else {
319 auto vol = VolumeManager::Instance()->findVolume(volId);
320 if (vol == nullptr) {
321 return error("Failed to find volume " + volId);
322 }
323 if (vol->getType() != VolumeBase::Type::kPrivate) {
324 return error("Volume " + volId + " not private");
325 }
326 if (vol->getState() != VolumeBase::State::kMounted) {
327 return error("Volume " + volId + " not mounted");
328 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600329 *path = vol->getPath();
330 if (path->empty()) {
331 return error("Volume " + volId + " missing path");
332 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600333 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700334 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600335}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600336
Jeff Sharkey2048a282017-06-15 09:59:43 -0600337binder::Status VoldNativeService::benchmark(
Songchun Fan01c4e912020-03-03 17:58:20 -0800338 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100339 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600340 CHECK_ARGUMENT_ID(volId);
341 ACQUIRE_LOCK;
342
343 std::string path;
344 auto status = pathForVolId(volId, &path);
345 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600346
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700347 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700348 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600349}
350
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700351binder::Status VoldNativeService::moveStorage(
Songchun Fan01c4e912020-03-03 17:58:20 -0800352 const std::string& fromVolId, const std::string& toVolId,
353 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100354 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600355 CHECK_ARGUMENT_ID(fromVolId);
356 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600357 ACQUIRE_LOCK;
358
359 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
360 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
361 if (fromVol == nullptr) {
362 return error("Failed to find volume " + fromVolId);
363 } else if (toVol == nullptr) {
364 return error("Failed to find volume " + toVolId);
365 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600366
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700367 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700368 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600369}
370
371binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100372 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600373 ACQUIRE_LOCK;
374
Sudheer Shanka817b9112018-12-13 17:40:28 -0800375 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600376}
377
Ricky Wai6b122572020-02-28 16:30:47 +0000378binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
379 const std::vector<std::string>& packageNames) {
380 ENFORCE_SYSTEM_OR_ROOT;
381 ACQUIRE_LOCK;
382
Ricky Waia2ca11e2021-01-15 14:03:23 +0000383 return translate(VolumeManager::Instance()->handleAppStorageDirs(uid, pid,
384 false /* doUnmount */, packageNames));
385}
386
387binder::Status VoldNativeService::unmountAppStorageDirs(int uid, int pid,
388 const std::vector<std::string>& packageNames) {
389 ENFORCE_SYSTEM_OR_ROOT;
390 ACQUIRE_LOCK;
391
392 return translate(VolumeManager::Instance()->handleAppStorageDirs(uid, pid,
393 true /* doUnmount */, packageNames));
Ricky Wai6b122572020-02-28 16:30:47 +0000394}
395
Martijn Coenen8a68a072020-02-12 15:29:02 +0100396binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100397 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600398 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600399 ACQUIRE_LOCK;
400
Martijn Coenen8a68a072020-02-12 15:29:02 +0100401 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600402}
403
Ricky Waibbfb6ea2020-12-03 15:32:52 +0000404binder::Status VoldNativeService::ensureAppDirsCreated(const std::vector<std::string>& paths,
405 int32_t appUid) {
406 ENFORCE_SYSTEM_OR_ROOT;
407 ACQUIRE_LOCK;
408
409 return translate(VolumeManager::Instance()->ensureAppDirsCreated(paths, appUid));
410}
411
Martijn Coenen816f4d92020-02-18 15:06:37 +0100412binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
413 ENFORCE_SYSTEM_OR_ROOT;
414 CHECK_ARGUMENT_PATH(path);
415 ACQUIRE_LOCK;
416
417 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
418}
419
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600420binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700421 const std::string& sourceKey, int32_t ownerGid,
422 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100423 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600424 CHECK_ARGUMENT_PATH(sourcePath);
425 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600426 ACQUIRE_LOCK;
427
428 return translate(
Songchun Fan01c4e912020-03-03 17:58:20 -0800429 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600430}
431
432binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100433 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600434 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600435 ACQUIRE_LOCK;
436
437 return translate(VolumeManager::Instance()->destroyObb(volId));
438}
439
Risan82e90de2020-02-04 16:07:21 +0900440binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
441 const std::string& mountPath,
442 const std::string& fsType,
443 const std::string& fsUuid,
444 const std::string& fsLabel, int32_t flags,
445 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100446 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900447 CHECK_ARGUMENT_PATH(sourcePath);
448 CHECK_ARGUMENT_PATH(mountPath);
449 CHECK_ARGUMENT_HEX(fsUuid);
450 // Label limitation seems to be different between fs (including allowed characters), so checking
451 // is quite meaningless.
452 ACQUIRE_LOCK;
453
Risan82e90de2020-02-04 16:07:21 +0900454 return translate(VolumeManager::Instance()->createStubVolume(
455 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900456}
457
458binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100459 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900460 CHECK_ARGUMENT_ID(volId);
461 ACQUIRE_LOCK;
462
463 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
464}
465
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700466binder::Status VoldNativeService::fstrim(
Songchun Fan01c4e912020-03-03 17:58:20 -0800467 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100468 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600469 ACQUIRE_LOCK;
470
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700471 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700472 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600473}
474
Jin Qiana370c142017-10-17 15:41:45 -0700475binder::Status VoldNativeService::runIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800476 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100477 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700478 ACQUIRE_LOCK;
479
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700480 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700481 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700482}
483
484binder::Status VoldNativeService::abortIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800485 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100486 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700487 ACQUIRE_LOCK;
488
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700489 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700490 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700491}
492
Risan8f6198d2018-10-26 20:56:45 -0600493binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700494 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100495 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600496 ACQUIRE_LOCK;
497
Risan8f6198d2018-10-26 20:56:45 -0600498 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600499}
500
Risan8f6198d2018-10-26 20:56:45 -0600501binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100502 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600503 ACQUIRE_LOCK;
504
Risan8f6198d2018-10-26 20:56:45 -0600505 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
506}
507
508binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
509 int32_t flags,
510 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100511 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600512 ACQUIRE_LOCK;
513
514 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
515 if (fd == -1) {
516 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
517 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
518 " flags: " + std::to_string(flags));
519 }
520
521 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700522 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600523}
524
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600525binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100526 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600527 ACQUIRE_CRYPT_LOCK;
528
529 return translate(cryptfs_check_passwd(password.c_str()));
530}
531
532binder::Status VoldNativeService::fdeRestart() {
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 // Spawn as thread so init can issue commands back to vold without
537 // causing deadlock, usually as a result of prep_data_fs.
538 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700539 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600540}
541
542binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100543 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600544 ACQUIRE_CRYPT_LOCK;
545
546 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700547 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600548}
549
550static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700551 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600552 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
553
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600554 for (int tries = 0; tries < 2; ++tries) {
555 int rc;
556 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800557 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600558 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800559 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600560 }
561
562 if (rc == 0) {
563 return 0;
564 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600565 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600566 }
567 }
568
569 return -1;
570}
571
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700572binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
573 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100574 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600575 ACQUIRE_CRYPT_LOCK;
576
Paul Crowley0fd26262018-01-30 09:48:19 -0800577 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700578 if (fscrypt_is_native()) {
579 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
580 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600581 }
Eric Biggersa701c452018-10-23 13:06:55 -0700582 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600583
584 // Spawn as thread so init can issue commands back to vold without
585 // causing deadlock, usually as a result of prep_data_fs.
586 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700587 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600588}
589
590binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700591 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100592 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600593 ACQUIRE_CRYPT_LOCK;
594
595 return translate(cryptfs_changepw(passwordType, password.c_str()));
596}
597
598binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100599 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600600 ACQUIRE_CRYPT_LOCK;
601
602 return translate(cryptfs_verify_passwd(password.c_str()));
603}
604
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700605binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100606 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600607 ACQUIRE_CRYPT_LOCK;
608
609 char buf[PROPERTY_VALUE_MAX];
610 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
611 return error(StringPrintf("Failed to read field %s", key.c_str()));
612 } else {
613 *_aidl_return = buf;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700614 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600615 }
616}
617
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700618binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100619 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600620 ACQUIRE_CRYPT_LOCK;
621
622 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
623}
624
625binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100626 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600627 ACQUIRE_CRYPT_LOCK;
628
629 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700630 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600631}
632
633binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100634 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600635 ACQUIRE_CRYPT_LOCK;
636
637 const char* res = cryptfs_get_password();
638 if (res != nullptr) {
639 *_aidl_return = res;
640 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700641 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600642}
643
644binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100645 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600646 ACQUIRE_CRYPT_LOCK;
647
648 cryptfs_clear_password();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700649 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600650}
651
652binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100653 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600654 ACQUIRE_CRYPT_LOCK;
655
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700656 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600657}
658
659binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100660 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600661 ACQUIRE_CRYPT_LOCK;
662
Eric Biggersa701c452018-10-23 13:06:55 -0700663 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600664 // Spawn as thread so init can issue commands back to vold without
665 // causing deadlock, usually as a result of prep_data_fs.
666 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600667 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700668 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600669}
670
671binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100672 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600673 ACQUIRE_CRYPT_LOCK;
674
Eric Biggersa701c452018-10-23 13:06:55 -0700675 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600676}
677
678binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100679 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600680 ACQUIRE_CRYPT_LOCK;
681
682 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700683 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600684}
685
Paul Lawrence236e5e82019-06-25 14:44:33 -0700686binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
687 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100688 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800689 ACQUIRE_LOCK;
690
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800691 return translateBool(
692 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false, "null"));
Paul Crowley0fd26262018-01-30 09:48:19 -0800693}
694
Paul Lawrence236e5e82019-06-25 14:44:33 -0700695binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800696 const std::string& mountPoint, bool shouldFormat,
697 const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100698 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800699 ACQUIRE_LOCK;
700
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800701 return translateBool(
702 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat, fsType));
Paul Crowley0fd26262018-01-30 09:48:19 -0800703}
704
Seth Moore5a43d612021-01-19 17:51:51 +0000705binder::Status VoldNativeService::setStorageBindingSeed(const std::vector<uint8_t>& seed) {
706 ENFORCE_SYSTEM_OR_ROOT;
707 ACQUIRE_CRYPT_LOCK;
708
709 return translateBool(setKeyStorageBindingSeed(seed));
710}
711
Songchun Fan01c4e912020-03-03 17:58:20 -0800712binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
713 bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100714 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600715 ACQUIRE_CRYPT_LOCK;
716
Eric Biggersa701c452018-10-23 13:06:55 -0700717 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600718}
719
720binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100721 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600722 ACQUIRE_CRYPT_LOCK;
723
Eric Biggersa701c452018-10-23 13:06:55 -0700724 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600725}
726
Satya Tangiralae1361712021-03-15 15:33:08 -0700727static bool token_empty(const std::string& token) {
728 return token.size() == 0 || token == "!";
729}
730
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600731binder::Status VoldNativeService::addUserKeyAuth(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
Satya Tangiralae1361712021-03-15 15:33:08 -0700737 if (!token_empty(token)) {
738 LOG(ERROR) << "Vold doesn't use auth tokens, but non-empty token passed to addUserKeyAuth.";
739 return binder::Status::fromServiceSpecificError(-EINVAL);
740 }
741
742 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600743}
744
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700745binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
746 const std::string& token,
747 const std::string& secret) {
748 ENFORCE_SYSTEM_OR_ROOT;
749 ACQUIRE_CRYPT_LOCK;
750
Satya Tangiralae1361712021-03-15 15:33:08 -0700751 if (!token_empty(token)) {
752 LOG(ERROR)
753 << "Vold doesn't use auth tokens, but non-empty token passed to clearUserKeyAuth.";
754 return binder::Status::fromServiceSpecificError(-EINVAL);
755 }
756
757 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, secret));
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700758}
759
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600760binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100761 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600762 ACQUIRE_CRYPT_LOCK;
763
Eric Biggersa701c452018-10-23 13:06:55 -0700764 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600765}
766
767binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700768 const std::string& token,
769 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100770 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600771 ACQUIRE_CRYPT_LOCK;
772
Satya Tangiralae1361712021-03-15 15:33:08 -0700773 if (!token_empty(token)) {
774 LOG(ERROR) << "Vold doesn't use auth tokens, but non-empty token passed to unlockUserKey.";
775 return binder::Status::fromServiceSpecificError(-EINVAL);
776 }
777
778 return translateBool(fscrypt_unlock_user_key(userId, userSerial, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600779}
780
781binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100782 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600783 ACQUIRE_CRYPT_LOCK;
784
Eric Biggersa701c452018-10-23 13:06:55 -0700785 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600786}
787
Jooyung Han4fcb7072020-01-23 13:23:26 +0900788binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700789 int32_t userId, int32_t userSerial,
790 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100791 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700792 std::string empty_string = "";
793 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700794 CHECK_ARGUMENT_HEX(uuid_);
795
796 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700797 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600798}
799
Jooyung Han4fcb7072020-01-23 13:23:26 +0900800binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700801 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100802 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700803 std::string empty_string = "";
804 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700805 CHECK_ARGUMENT_HEX(uuid_);
806
807 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700808 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600809}
810
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700811binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
812 int32_t appId, const std::string& sandboxId,
813 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700814 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700815}
816
817binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700818 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700819 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700820 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700821}
822
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700823binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100824 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700825 ACQUIRE_LOCK;
826
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700827 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700828}
829
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700830binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100831 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700832 ACQUIRE_LOCK;
833
834 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700835 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700836}
837
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700838binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100839 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700840 ACQUIRE_LOCK;
841
842 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700843 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700844}
845
David Anderson23850d32020-06-10 23:51:17 -0700846binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
847 ENFORCE_SYSTEM_OR_ROOT;
848 ACQUIRE_LOCK;
849
850 *_aidl_return = cp_isCheckpointing();
851 return Ok();
852}
853
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700854binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100855 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700856 ACQUIRE_LOCK;
857
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700858 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700859}
860
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700861binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100862 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700863 ACQUIRE_LOCK;
864
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700865 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700866}
867
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700868binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100869 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700870 CHECK_ARGUMENT_PATH(mountPoint);
871 ACQUIRE_LOCK;
872
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700873 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700874}
875
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800876binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100877 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800878 CHECK_ARGUMENT_PATH(mountPoint);
879 ACQUIRE_LOCK;
880
881 return cp_restoreCheckpoint(mountPoint, count);
882}
883
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700884binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100885 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700886 ACQUIRE_LOCK;
887
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700888 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700889}
890
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700891binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100892 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700893 ACQUIRE_LOCK;
894
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700895 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700896 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700897}
898
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800899binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100900 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800901 ACQUIRE_LOCK;
902
903 return cp_supportsCheckpoint(*_aidl_return);
904}
905
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700906binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100907 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700908 ACQUIRE_LOCK;
909
910 return cp_supportsBlockCheckpoint(*_aidl_return);
911}
912
913binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100914 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700915 ACQUIRE_LOCK;
916
917 return cp_supportsFileCheckpoint(*_aidl_return);
918}
919
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100920binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100921 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100922 ACQUIRE_LOCK;
923
924 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700925 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100926}
927
Alex Buynytskyyb02e7b62021-02-11 19:09:10 -0800928static void initializeIncFs() {
929 // Obtaining IncFS features triggers initialization of IncFS.
930 incfs::features();
931}
932
Martijn Coeneneed957f2020-11-12 10:59:13 +0100933binder::Status VoldNativeService::earlyBootEnded() {
934 ENFORCE_SYSTEM_OR_ROOT;
935 ACQUIRE_LOCK;
936
Alex Buynytskyyb02e7b62021-02-11 19:09:10 -0800937 initializeIncFs();
Martijn Coeneneed957f2020-11-12 10:59:13 +0100938 Keymaster::earlyBootEnded();
939 return Ok();
940}
941
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800942binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700943 ENFORCE_SYSTEM_OR_ROOT;
944
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700945 *_aidl_return = incfs::enabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700946 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800947}
948
949binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800950 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800951 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700952 ENFORCE_SYSTEM_OR_ROOT;
953 CHECK_ARGUMENT_PATH(backingPath);
954 CHECK_ARGUMENT_PATH(targetDir);
955
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700956 auto control = incfs::mount(backingPath, targetDir,
957 {.flags = IncFsMountFlags(flags),
958 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
959 // Mount with read logs disabled.
960 .readLogBufferPages = 0});
961 if (!control) {
962 return translate(-errno);
Songchun Fanab55cec2019-12-02 10:50:12 -0800963 }
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700964 auto fds = control.releaseFds();
965 using android::base::unique_fd;
966 _aidl_return->cmd.reset(unique_fd(fds[CMD].release()));
967 _aidl_return->pendingReads.reset(unique_fd(fds[PENDING_READS].release()));
968 _aidl_return->log.reset(unique_fd(fds[LOGS].release()));
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700969 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800970}
971
972binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700973 ENFORCE_SYSTEM_OR_ROOT;
974 CHECK_ARGUMENT_PATH(dir);
975
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700976 return translate(incfs::unmount(dir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800977}
978
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700979binder::Status VoldNativeService::setIncFsMountOptions(
980 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
981 bool enableReadLogs) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -0700982 ENFORCE_SYSTEM_OR_ROOT;
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700983
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700984 auto incfsControl =
985 incfs::createControl(control.cmd.get(), control.pendingReads.get(), control.log.get());
986 auto cleanupFunc = [](auto incfsControl) {
987 for (auto& fd : incfsControl->releaseFds()) {
988 (void)fd.release();
989 }
990 };
991 auto cleanup =
992 std::unique_ptr<incfs::Control, decltype(cleanupFunc)>(&incfsControl, cleanupFunc);
993 if (auto error = incfs::setOptions(
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700994 incfsControl,
995 {.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
996 .readLogBufferPages = enableReadLogs ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES : 0});
997 error < 0) {
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700998 return binder::Status::fromServiceSpecificError(error);
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700999 }
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -07001000
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -07001001 return Ok();
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -07001002}
1003
Songchun Fanab55cec2019-12-02 10:50:12 -08001004binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
1005 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001006 ENFORCE_SYSTEM_OR_ROOT;
1007 CHECK_ARGUMENT_PATH(sourceDir);
1008 CHECK_ARGUMENT_PATH(targetDir);
1009
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -07001010 return translate(incfs::bindMount(sourceDir, targetDir));
Songchun Fanab55cec2019-12-02 10:50:12 -08001011}
1012
Yo Chiang0af25a32020-10-07 14:20:00 +08001013binder::Status VoldNativeService::destroyDsuMetadataKey(const std::string& dsuSlot) {
1014 ENFORCE_SYSTEM_OR_ROOT;
1015 ACQUIRE_LOCK;
1016
1017 return translateBool(destroy_dsu_metadata_key(dsuSlot));
1018}
1019
Jeff Sharkey068c6be2017-09-06 13:47:40 -06001020} // namespace vold
1021} // namespace android