blob: 3fb4e65bf86eff7701eee2e277ba73fe1fd40e61 [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"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060036#include "MetadataCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080037#include "MoveStorage.h"
38#include "Process.h"
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070039#include "VoldNativeServiceValidation.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080040#include "VoldUtil.h"
41#include "VolumeManager.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070042#include "cryptfs.h"
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -070043#include "incfs.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060044
Jeff Sharkey068c6be2017-09-06 13:47:40 -060045using android::base::StringPrintf;
46using std::endl;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070047using namespace std::literals;
Jeff Sharkey068c6be2017-09-06 13:47:40 -060048
49namespace android {
50namespace vold {
51
52namespace {
53
54constexpr const char* kDump = "android.permission.DUMP";
55
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060056static binder::Status error(const std::string& msg) {
57 PLOG(ERROR) << msg;
58 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
59}
60
Jeff Sharkey83b559c2017-09-12 16:30:52 -060061static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060062 if (status == 0) {
63 return binder::Status::ok();
64 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060065 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060066 }
67}
68
Jeff Sharkey83b559c2017-09-12 16:30:52 -060069static binder::Status translateBool(bool status) {
70 if (status) {
71 return binder::Status::ok();
72 } else {
73 return binder::Status::fromServiceSpecificError(status);
74 }
75}
76
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010077#define ENFORCE_SYSTEM_OR_ROOT \
78 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070079 binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010080 if (!status.isOk()) { \
81 return status; \
82 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070083 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -060084
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070085#define CHECK_ARGUMENT_ID(id) \
86 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070087 binder::Status status = CheckArgumentId((id)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070088 if (!status.isOk()) { \
89 return status; \
90 } \
91 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -060092
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070093#define CHECK_ARGUMENT_PATH(path) \
94 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070095 binder::Status status = CheckArgumentPath((path)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070096 if (!status.isOk()) { \
97 return status; \
98 } \
99 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600100
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700101#define CHECK_ARGUMENT_HEX(hex) \
102 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700103 binder::Status status = CheckArgumentHex((hex)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700104 if (!status.isOk()) { \
105 return status; \
106 } \
107 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600108
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700109#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600110 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
111 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600112
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700113#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600114 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
115 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600116
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600117} // namespace
118
119status_t VoldNativeService::start() {
120 IPCThreadState::self()->disableBackgroundScheduling(true);
121 status_t ret = BinderService<VoldNativeService>::publish();
122 if (ret != android::OK) {
123 return ret;
124 }
125 sp<ProcessState> ps(ProcessState::self());
126 ps->startThreadPool();
127 ps->giveThreadPoolName();
128 return android::OK;
129}
130
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700131status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600132 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700133 const binder::Status dump_permission = CheckPermission(kDump);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600134 if (!dump_permission.isOk()) {
135 out << dump_permission.toString8() << endl;
136 return PERMISSION_DENIED;
137 }
138
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600139 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600140 out << "vold is happy!" << endl;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600141 return NO_ERROR;
142}
143
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600144binder::Status VoldNativeService::setListener(
Songchun Fan01c4e912020-03-03 17:58:20 -0800145 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100146 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600147 ACQUIRE_LOCK;
148
149 VolumeManager::Instance()->setListener(listener);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700150 return Ok();
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600151}
152
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600153binder::Status VoldNativeService::monitor() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100154 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600155
156 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700157 { ACQUIRE_LOCK; }
158 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600159
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700160 return Ok();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600161}
162
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600163binder::Status VoldNativeService::reset() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100164 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600165 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600166
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600167 return translate(VolumeManager::Instance()->reset());
168}
169
170binder::Status VoldNativeService::shutdown() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100171 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600172 ACQUIRE_LOCK;
173
174 return translate(VolumeManager::Instance()->shutdown());
175}
176
Martijn Coenen23c04452020-04-29 07:49:41 +0200177binder::Status VoldNativeService::abortFuse() {
178 ENFORCE_SYSTEM_OR_ROOT;
179 ACQUIRE_LOCK;
180
181 return translate(VolumeManager::Instance()->abortFuse());
182}
183
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600184binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100185 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600186 ACQUIRE_LOCK;
187
188 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
189}
190
191binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100192 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600193 ACQUIRE_LOCK;
194
195 return translate(VolumeManager::Instance()->onUserRemoved(userId));
196}
197
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700198binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100199 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600200 ACQUIRE_LOCK;
201
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700202 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600203}
204
205binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100206 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600207 ACQUIRE_LOCK;
208
209 return translate(VolumeManager::Instance()->onUserStopped(userId));
210}
211
Sudheer Shankad484aa92018-07-31 10:07:34 -0700212binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700213 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700214 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700215}
216
Sudheer Shankad484aa92018-07-31 10:07:34 -0700217binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700218 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700219 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700220}
221
Jeff Sharkey401b2602017-12-14 22:15:20 -0700222binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100223 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700224 ACQUIRE_LOCK;
225
226 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
227}
228
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600229binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700230 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100231 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600232 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600233 ACQUIRE_LOCK;
234
235 auto disk = VolumeManager::Instance()->findDisk(diskId);
236 if (disk == nullptr) {
237 return error("Failed to find disk " + diskId);
238 }
239 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700240 case PARTITION_TYPE_PUBLIC:
241 return translate(disk->partitionPublic());
242 case PARTITION_TYPE_PRIVATE:
243 return translate(disk->partitionPrivate());
244 case PARTITION_TYPE_MIXED:
245 return translate(disk->partitionMixed(ratio));
246 default:
247 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600248 }
249}
250
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600251binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700252 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100253 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600254 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600255 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600256 ACQUIRE_LOCK;
257
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600258 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600259}
260
Zim5048b4b2019-11-19 09:16:03 +0000261binder::Status VoldNativeService::mount(
262 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
263 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100264 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600265 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600266 ACQUIRE_LOCK;
267
268 auto vol = VolumeManager::Instance()->findVolume(volId);
269 if (vol == nullptr) {
270 return error("Failed to find volume " + volId);
271 }
272
273 vol->setMountFlags(mountFlags);
274 vol->setMountUserId(mountUserId);
275
Zim5048b4b2019-11-19 09:16:03 +0000276 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600277 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000278 vol->setMountCallback(nullptr);
279
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700280 if (res != OK) {
281 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600282 }
Zim3623a212019-07-19 16:46:53 +0100283
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700284 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
285 res = VolumeManager::Instance()->setPrimary(vol);
286 if (res != OK) {
287 return translate(res);
288 }
289 }
290 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600291}
292
293binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100294 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600295 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600296 ACQUIRE_LOCK;
297
298 auto vol = VolumeManager::Instance()->findVolume(volId);
299 if (vol == nullptr) {
300 return error("Failed to find volume " + volId);
301 }
302 return translate(vol->unmount());
303}
304
305binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100306 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600307 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600308 ACQUIRE_LOCK;
309
310 auto vol = VolumeManager::Instance()->findVolume(volId);
311 if (vol == nullptr) {
312 return error("Failed to find volume " + volId);
313 }
314 return translate(vol->format(fsType));
315}
316
Jeff Sharkey2048a282017-06-15 09:59:43 -0600317static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600318 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600319 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600320 } else {
321 auto vol = VolumeManager::Instance()->findVolume(volId);
322 if (vol == nullptr) {
323 return error("Failed to find volume " + volId);
324 }
325 if (vol->getType() != VolumeBase::Type::kPrivate) {
326 return error("Volume " + volId + " not private");
327 }
328 if (vol->getState() != VolumeBase::State::kMounted) {
329 return error("Volume " + volId + " not mounted");
330 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600331 *path = vol->getPath();
332 if (path->empty()) {
333 return error("Volume " + volId + " missing path");
334 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600335 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700336 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600337}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600338
Jeff Sharkey2048a282017-06-15 09:59:43 -0600339binder::Status VoldNativeService::benchmark(
Songchun Fan01c4e912020-03-03 17:58:20 -0800340 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100341 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600342 CHECK_ARGUMENT_ID(volId);
343 ACQUIRE_LOCK;
344
345 std::string path;
346 auto status = pathForVolId(volId, &path);
347 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600348
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700349 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700350 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600351}
352
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700353binder::Status VoldNativeService::moveStorage(
Songchun Fan01c4e912020-03-03 17:58:20 -0800354 const std::string& fromVolId, const std::string& toVolId,
355 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100356 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600357 CHECK_ARGUMENT_ID(fromVolId);
358 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600359 ACQUIRE_LOCK;
360
361 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
362 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
363 if (fromVol == nullptr) {
364 return error("Failed to find volume " + fromVolId);
365 } else if (toVol == nullptr) {
366 return error("Failed to find volume " + toVolId);
367 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600368
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700369 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700370 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600371}
372
373binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100374 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600375 ACQUIRE_LOCK;
376
Sudheer Shanka817b9112018-12-13 17:40:28 -0800377 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600378}
379
Ricky Wai6b122572020-02-28 16:30:47 +0000380binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
381 const std::vector<std::string>& packageNames) {
382 ENFORCE_SYSTEM_OR_ROOT;
383 ACQUIRE_LOCK;
384
385 return translate(VolumeManager::Instance()->remountAppStorageDirs(uid, pid, packageNames));
386}
387
Martijn Coenen8a68a072020-02-12 15:29:02 +0100388binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100389 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600390 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600391 ACQUIRE_LOCK;
392
Martijn Coenen8a68a072020-02-12 15:29:02 +0100393 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600394}
395
Martijn Coenen816f4d92020-02-18 15:06:37 +0100396binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
397 ENFORCE_SYSTEM_OR_ROOT;
398 CHECK_ARGUMENT_PATH(path);
399 ACQUIRE_LOCK;
400
401 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
402}
403
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600404binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700405 const std::string& sourceKey, int32_t ownerGid,
406 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100407 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600408 CHECK_ARGUMENT_PATH(sourcePath);
409 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600410 ACQUIRE_LOCK;
411
412 return translate(
Songchun Fan01c4e912020-03-03 17:58:20 -0800413 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600414}
415
416binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100417 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600418 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600419 ACQUIRE_LOCK;
420
421 return translate(VolumeManager::Instance()->destroyObb(volId));
422}
423
Risan82e90de2020-02-04 16:07:21 +0900424binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
425 const std::string& mountPath,
426 const std::string& fsType,
427 const std::string& fsUuid,
428 const std::string& fsLabel, int32_t flags,
429 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100430 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900431 CHECK_ARGUMENT_PATH(sourcePath);
432 CHECK_ARGUMENT_PATH(mountPath);
433 CHECK_ARGUMENT_HEX(fsUuid);
434 // Label limitation seems to be different between fs (including allowed characters), so checking
435 // is quite meaningless.
436 ACQUIRE_LOCK;
437
Risan82e90de2020-02-04 16:07:21 +0900438 return translate(VolumeManager::Instance()->createStubVolume(
439 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900440}
441
442binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100443 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900444 CHECK_ARGUMENT_ID(volId);
445 ACQUIRE_LOCK;
446
447 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
448}
449
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700450binder::Status VoldNativeService::fstrim(
Songchun Fan01c4e912020-03-03 17:58:20 -0800451 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100452 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600453 ACQUIRE_LOCK;
454
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700455 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700456 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600457}
458
Jin Qiana370c142017-10-17 15:41:45 -0700459binder::Status VoldNativeService::runIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800460 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100461 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700462 ACQUIRE_LOCK;
463
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700464 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700465 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700466}
467
468binder::Status VoldNativeService::abortIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800469 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100470 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700471 ACQUIRE_LOCK;
472
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700473 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700474 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700475}
476
Risan8f6198d2018-10-26 20:56:45 -0600477binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700478 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100479 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600480 ACQUIRE_LOCK;
481
Risan8f6198d2018-10-26 20:56:45 -0600482 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600483}
484
Risan8f6198d2018-10-26 20:56:45 -0600485binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100486 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600487 ACQUIRE_LOCK;
488
Risan8f6198d2018-10-26 20:56:45 -0600489 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
490}
491
492binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
493 int32_t flags,
494 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100495 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600496 ACQUIRE_LOCK;
497
498 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
499 if (fd == -1) {
500 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
501 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
502 " flags: " + std::to_string(flags));
503 }
504
505 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700506 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600507}
508
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600509binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100510 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600511 ACQUIRE_CRYPT_LOCK;
512
513 return translate(cryptfs_check_passwd(password.c_str()));
514}
515
516binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100517 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600518 ACQUIRE_CRYPT_LOCK;
519
520 // Spawn as thread so init can issue commands back to vold without
521 // causing deadlock, usually as a result of prep_data_fs.
522 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700523 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600524}
525
526binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100527 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600528 ACQUIRE_CRYPT_LOCK;
529
530 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700531 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600532}
533
534static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700535 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600536 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
537
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600538 for (int tries = 0; tries < 2; ++tries) {
539 int rc;
540 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800541 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600542 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800543 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600544 }
545
546 if (rc == 0) {
547 return 0;
548 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600549 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600550 }
551 }
552
553 return -1;
554}
555
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700556binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
557 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100558 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600559 ACQUIRE_CRYPT_LOCK;
560
Paul Crowley0fd26262018-01-30 09:48:19 -0800561 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700562 if (fscrypt_is_native()) {
563 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
564 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600565 }
Eric Biggersa701c452018-10-23 13:06:55 -0700566 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600567
568 // Spawn as thread so init can issue commands back to vold without
569 // causing deadlock, usually as a result of prep_data_fs.
570 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700571 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600572}
573
574binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700575 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100576 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600577 ACQUIRE_CRYPT_LOCK;
578
579 return translate(cryptfs_changepw(passwordType, password.c_str()));
580}
581
582binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100583 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600584 ACQUIRE_CRYPT_LOCK;
585
586 return translate(cryptfs_verify_passwd(password.c_str()));
587}
588
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700589binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
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 char buf[PROPERTY_VALUE_MAX];
594 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
595 return error(StringPrintf("Failed to read field %s", key.c_str()));
596 } else {
597 *_aidl_return = buf;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700598 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600599 }
600}
601
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700602binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100603 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600604 ACQUIRE_CRYPT_LOCK;
605
606 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
607}
608
609binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100610 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600611 ACQUIRE_CRYPT_LOCK;
612
613 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700614 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600615}
616
617binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100618 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600619 ACQUIRE_CRYPT_LOCK;
620
621 const char* res = cryptfs_get_password();
622 if (res != nullptr) {
623 *_aidl_return = res;
624 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700625 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600626}
627
628binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100629 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600630 ACQUIRE_CRYPT_LOCK;
631
632 cryptfs_clear_password();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700633 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600634}
635
636binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100637 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600638 ACQUIRE_CRYPT_LOCK;
639
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700640 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600641}
642
643binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100644 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600645 ACQUIRE_CRYPT_LOCK;
646
Eric Biggersa701c452018-10-23 13:06:55 -0700647 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648 // Spawn as thread so init can issue commands back to vold without
649 // causing deadlock, usually as a result of prep_data_fs.
650 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600651 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700652 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600653}
654
655binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100656 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600657 ACQUIRE_CRYPT_LOCK;
658
Eric Biggersa701c452018-10-23 13:06:55 -0700659 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600660}
661
662binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100663 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600664 ACQUIRE_CRYPT_LOCK;
665
666 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700667 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600668}
669
Paul Lawrence236e5e82019-06-25 14:44:33 -0700670binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
671 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100672 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800673 ACQUIRE_LOCK;
674
Paul Lawrence236e5e82019-06-25 14:44:33 -0700675 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800676}
677
Paul Lawrence236e5e82019-06-25 14:44:33 -0700678binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
679 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100680 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800681 ACQUIRE_LOCK;
682
Paul Lawrence236e5e82019-06-25 14:44:33 -0700683 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800684}
685
Songchun Fan01c4e912020-03-03 17:58:20 -0800686binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
687 bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100688 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600689 ACQUIRE_CRYPT_LOCK;
690
Eric Biggersa701c452018-10-23 13:06:55 -0700691 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600692}
693
694binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100695 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600696 ACQUIRE_CRYPT_LOCK;
697
Eric Biggersa701c452018-10-23 13:06:55 -0700698 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600699}
700
701binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700702 const std::string& token,
703 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100704 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705 ACQUIRE_CRYPT_LOCK;
706
Eric Biggersa701c452018-10-23 13:06:55 -0700707 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600708}
709
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700710binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
711 const std::string& token,
712 const std::string& secret) {
713 ENFORCE_SYSTEM_OR_ROOT;
714 ACQUIRE_CRYPT_LOCK;
715
716 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
717}
718
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600719binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100720 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721 ACQUIRE_CRYPT_LOCK;
722
Eric Biggersa701c452018-10-23 13:06:55 -0700723 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724}
725
726binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700727 const std::string& token,
728 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100729 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600730 ACQUIRE_CRYPT_LOCK;
731
Eric Biggersa701c452018-10-23 13:06:55 -0700732 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600733}
734
735binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100736 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600737 ACQUIRE_CRYPT_LOCK;
738
Eric Biggersa701c452018-10-23 13:06:55 -0700739 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600740}
741
Jooyung Han4fcb7072020-01-23 13:23:26 +0900742binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700743 int32_t userId, int32_t userSerial,
744 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100745 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700746 std::string empty_string = "";
747 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700748 CHECK_ARGUMENT_HEX(uuid_);
749
750 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700751 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600752}
753
Jooyung Han4fcb7072020-01-23 13:23:26 +0900754binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700755 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100756 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700757 std::string empty_string = "";
758 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700759 CHECK_ARGUMENT_HEX(uuid_);
760
761 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700762 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600763}
764
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700765binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
766 int32_t appId, const std::string& sandboxId,
767 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700768 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700769}
770
771binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700772 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700773 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700774 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700775}
776
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700777binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100778 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700779 ACQUIRE_LOCK;
780
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700781 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700782}
783
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700784binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100785 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700786 ACQUIRE_LOCK;
787
788 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700789 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700790}
791
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700792binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100793 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700794 ACQUIRE_LOCK;
795
796 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700797 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700798}
799
David Anderson23850d32020-06-10 23:51:17 -0700800binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
801 ENFORCE_SYSTEM_OR_ROOT;
802 ACQUIRE_LOCK;
803
804 *_aidl_return = cp_isCheckpointing();
805 return Ok();
806}
807
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700808binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100809 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700810 ACQUIRE_LOCK;
811
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700812 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700813}
814
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700815binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100816 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700817 ACQUIRE_LOCK;
818
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700819 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700820}
821
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700822binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100823 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700824 CHECK_ARGUMENT_PATH(mountPoint);
825 ACQUIRE_LOCK;
826
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700827 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700828}
829
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800830binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100831 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800832 CHECK_ARGUMENT_PATH(mountPoint);
833 ACQUIRE_LOCK;
834
835 return cp_restoreCheckpoint(mountPoint, count);
836}
837
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700838binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100839 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700840 ACQUIRE_LOCK;
841
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700842 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700843}
844
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700845binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100846 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700847 ACQUIRE_LOCK;
848
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700849 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700850 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700851}
852
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800853binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100854 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800855 ACQUIRE_LOCK;
856
857 return cp_supportsCheckpoint(*_aidl_return);
858}
859
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700860binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100861 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700862 ACQUIRE_LOCK;
863
864 return cp_supportsBlockCheckpoint(*_aidl_return);
865}
866
867binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100868 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700869 ACQUIRE_LOCK;
870
871 return cp_supportsFileCheckpoint(*_aidl_return);
872}
873
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100874binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100875 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100876 ACQUIRE_LOCK;
877
878 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700879 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100880}
881
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800882binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700883 ENFORCE_SYSTEM_OR_ROOT;
884
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700885 *_aidl_return = incfs::enabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700886 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800887}
888
889binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800890 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800891 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700892 ENFORCE_SYSTEM_OR_ROOT;
893 CHECK_ARGUMENT_PATH(backingPath);
894 CHECK_ARGUMENT_PATH(targetDir);
895
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700896 auto control = incfs::mount(backingPath, targetDir,
897 {.flags = IncFsMountFlags(flags),
898 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
899 // Mount with read logs disabled.
900 .readLogBufferPages = 0});
901 if (!control) {
902 return translate(-errno);
Songchun Fanab55cec2019-12-02 10:50:12 -0800903 }
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700904 auto fds = control.releaseFds();
905 using android::base::unique_fd;
906 _aidl_return->cmd.reset(unique_fd(fds[CMD].release()));
907 _aidl_return->pendingReads.reset(unique_fd(fds[PENDING_READS].release()));
908 _aidl_return->log.reset(unique_fd(fds[LOGS].release()));
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700909 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800910}
911
912binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700913 ENFORCE_SYSTEM_OR_ROOT;
914 CHECK_ARGUMENT_PATH(dir);
915
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700916 return translate(incfs::unmount(dir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800917}
918
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700919binder::Status VoldNativeService::setIncFsMountOptions(
920 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
921 bool enableReadLogs) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -0700922 ENFORCE_SYSTEM_OR_ROOT;
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700923
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700924 auto incfsControl =
925 incfs::createControl(control.cmd.get(), control.pendingReads.get(), control.log.get());
926 auto cleanupFunc = [](auto incfsControl) {
927 for (auto& fd : incfsControl->releaseFds()) {
928 (void)fd.release();
929 }
930 };
931 auto cleanup =
932 std::unique_ptr<incfs::Control, decltype(cleanupFunc)>(&incfsControl, cleanupFunc);
933 if (auto error = incfs::setOptions(
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700934 incfsControl,
935 {.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
936 .readLogBufferPages = enableReadLogs ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES : 0});
937 error < 0) {
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700938 return binder::Status::fromServiceSpecificError(error);
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700939 }
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700940
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700941 return Ok();
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700942}
943
Songchun Fanab55cec2019-12-02 10:50:12 -0800944binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
945 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700946 ENFORCE_SYSTEM_OR_ROOT;
947 CHECK_ARGUMENT_PATH(sourceDir);
948 CHECK_ARGUMENT_PATH(targetDir);
949
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700950 return translate(incfs::bindMount(sourceDir, targetDir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800951}
952
Yo Chiang0af25a32020-10-07 14:20:00 +0800953binder::Status VoldNativeService::destroyDsuMetadataKey(const std::string& dsuSlot) {
954 ENFORCE_SYSTEM_OR_ROOT;
955 ACQUIRE_LOCK;
956
957 return translateBool(destroy_dsu_metadata_key(dsuSlot));
958}
959
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600960} // namespace vold
961} // namespace android