blob: 81e7c633e2ef959541614931906d0dc238af1133 [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 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600285}
286
287binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100288 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600289 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600290 ACQUIRE_LOCK;
291
292 auto vol = VolumeManager::Instance()->findVolume(volId);
293 if (vol == nullptr) {
294 return error("Failed to find volume " + volId);
295 }
296 return translate(vol->unmount());
297}
298
299binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100300 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600301 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600302 ACQUIRE_LOCK;
303
304 auto vol = VolumeManager::Instance()->findVolume(volId);
305 if (vol == nullptr) {
306 return error("Failed to find volume " + volId);
307 }
308 return translate(vol->format(fsType));
309}
310
Jeff Sharkey2048a282017-06-15 09:59:43 -0600311static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600312 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600313 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600314 } else {
315 auto vol = VolumeManager::Instance()->findVolume(volId);
316 if (vol == nullptr) {
317 return error("Failed to find volume " + volId);
318 }
319 if (vol->getType() != VolumeBase::Type::kPrivate) {
320 return error("Volume " + volId + " not private");
321 }
322 if (vol->getState() != VolumeBase::State::kMounted) {
323 return error("Volume " + volId + " not mounted");
324 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600325 *path = vol->getPath();
326 if (path->empty()) {
327 return error("Volume " + volId + " missing path");
328 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600329 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700330 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600331}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600332
Jeff Sharkey2048a282017-06-15 09:59:43 -0600333binder::Status VoldNativeService::benchmark(
Songchun Fan01c4e912020-03-03 17:58:20 -0800334 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100335 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600336 CHECK_ARGUMENT_ID(volId);
337 ACQUIRE_LOCK;
338
339 std::string path;
340 auto status = pathForVolId(volId, &path);
341 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600342
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700343 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700344 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600345}
346
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700347binder::Status VoldNativeService::moveStorage(
Songchun Fan01c4e912020-03-03 17:58:20 -0800348 const std::string& fromVolId, const std::string& toVolId,
349 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100350 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600351 CHECK_ARGUMENT_ID(fromVolId);
352 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600353 ACQUIRE_LOCK;
354
355 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
356 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
357 if (fromVol == nullptr) {
358 return error("Failed to find volume " + fromVolId);
359 } else if (toVol == nullptr) {
360 return error("Failed to find volume " + toVolId);
361 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600362
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700363 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700364 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600365}
366
367binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100368 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600369 ACQUIRE_LOCK;
370
Sudheer Shanka817b9112018-12-13 17:40:28 -0800371 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600372}
373
Ricky Wai6b122572020-02-28 16:30:47 +0000374binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
375 const std::vector<std::string>& packageNames) {
376 ENFORCE_SYSTEM_OR_ROOT;
377 ACQUIRE_LOCK;
378
379 return translate(VolumeManager::Instance()->remountAppStorageDirs(uid, pid, packageNames));
380}
381
Martijn Coenen8a68a072020-02-12 15:29:02 +0100382binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100383 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600384 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600385 ACQUIRE_LOCK;
386
Martijn Coenen8a68a072020-02-12 15:29:02 +0100387 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600388}
389
Martijn Coenen816f4d92020-02-18 15:06:37 +0100390binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
391 ENFORCE_SYSTEM_OR_ROOT;
392 CHECK_ARGUMENT_PATH(path);
393 ACQUIRE_LOCK;
394
395 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
396}
397
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600398binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700399 const std::string& sourceKey, int32_t ownerGid,
400 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100401 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600402 CHECK_ARGUMENT_PATH(sourcePath);
403 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600404 ACQUIRE_LOCK;
405
406 return translate(
Songchun Fan01c4e912020-03-03 17:58:20 -0800407 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600408}
409
410binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100411 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600412 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600413 ACQUIRE_LOCK;
414
415 return translate(VolumeManager::Instance()->destroyObb(volId));
416}
417
Risan82e90de2020-02-04 16:07:21 +0900418binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
419 const std::string& mountPath,
420 const std::string& fsType,
421 const std::string& fsUuid,
422 const std::string& fsLabel, int32_t flags,
423 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100424 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900425 CHECK_ARGUMENT_PATH(sourcePath);
426 CHECK_ARGUMENT_PATH(mountPath);
427 CHECK_ARGUMENT_HEX(fsUuid);
428 // Label limitation seems to be different between fs (including allowed characters), so checking
429 // is quite meaningless.
430 ACQUIRE_LOCK;
431
Risan82e90de2020-02-04 16:07:21 +0900432 return translate(VolumeManager::Instance()->createStubVolume(
433 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900434}
435
436binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100437 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900438 CHECK_ARGUMENT_ID(volId);
439 ACQUIRE_LOCK;
440
441 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
442}
443
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700444binder::Status VoldNativeService::fstrim(
Songchun Fan01c4e912020-03-03 17:58:20 -0800445 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100446 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600447 ACQUIRE_LOCK;
448
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700449 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700450 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600451}
452
Jin Qiana370c142017-10-17 15:41:45 -0700453binder::Status VoldNativeService::runIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800454 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100455 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700456 ACQUIRE_LOCK;
457
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700458 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700459 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700460}
461
462binder::Status VoldNativeService::abortIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800463 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100464 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700465 ACQUIRE_LOCK;
466
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700467 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700468 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700469}
470
Risan8f6198d2018-10-26 20:56:45 -0600471binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700472 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100473 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600474 ACQUIRE_LOCK;
475
Risan8f6198d2018-10-26 20:56:45 -0600476 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600477}
478
Risan8f6198d2018-10-26 20:56:45 -0600479binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100480 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600481 ACQUIRE_LOCK;
482
Risan8f6198d2018-10-26 20:56:45 -0600483 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
484}
485
486binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
487 int32_t flags,
488 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100489 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600490 ACQUIRE_LOCK;
491
492 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
493 if (fd == -1) {
494 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
495 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
496 " flags: " + std::to_string(flags));
497 }
498
499 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700500 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600501}
502
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600503binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100504 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600505 ACQUIRE_CRYPT_LOCK;
506
507 return translate(cryptfs_check_passwd(password.c_str()));
508}
509
510binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100511 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600512 ACQUIRE_CRYPT_LOCK;
513
514 // Spawn as thread so init can issue commands back to vold without
515 // causing deadlock, usually as a result of prep_data_fs.
516 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700517 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600518}
519
520binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100521 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600522 ACQUIRE_CRYPT_LOCK;
523
524 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700525 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600526}
527
528static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700529 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600530 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
531
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600532 for (int tries = 0; tries < 2; ++tries) {
533 int rc;
534 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800535 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600536 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800537 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600538 }
539
540 if (rc == 0) {
541 return 0;
542 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600543 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600544 }
545 }
546
547 return -1;
548}
549
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700550binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
551 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100552 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600553 ACQUIRE_CRYPT_LOCK;
554
Paul Crowley0fd26262018-01-30 09:48:19 -0800555 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700556 if (fscrypt_is_native()) {
557 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
558 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600559 }
Eric Biggersa701c452018-10-23 13:06:55 -0700560 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600561
562 // Spawn as thread so init can issue commands back to vold without
563 // causing deadlock, usually as a result of prep_data_fs.
564 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700565 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600566}
567
568binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700569 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100570 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600571 ACQUIRE_CRYPT_LOCK;
572
573 return translate(cryptfs_changepw(passwordType, password.c_str()));
574}
575
576binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100577 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600578 ACQUIRE_CRYPT_LOCK;
579
580 return translate(cryptfs_verify_passwd(password.c_str()));
581}
582
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700583binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100584 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600585 ACQUIRE_CRYPT_LOCK;
586
587 char buf[PROPERTY_VALUE_MAX];
588 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
589 return error(StringPrintf("Failed to read field %s", key.c_str()));
590 } else {
591 *_aidl_return = buf;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700592 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600593 }
594}
595
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700596binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100597 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600598 ACQUIRE_CRYPT_LOCK;
599
600 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
601}
602
603binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100604 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600605 ACQUIRE_CRYPT_LOCK;
606
607 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700608 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600609}
610
611binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100612 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600613 ACQUIRE_CRYPT_LOCK;
614
615 const char* res = cryptfs_get_password();
616 if (res != nullptr) {
617 *_aidl_return = res;
618 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700619 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600620}
621
622binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100623 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600624 ACQUIRE_CRYPT_LOCK;
625
626 cryptfs_clear_password();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700627 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600628}
629
630binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100631 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600632 ACQUIRE_CRYPT_LOCK;
633
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700634 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600635}
636
637binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100638 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600639 ACQUIRE_CRYPT_LOCK;
640
Eric Biggersa701c452018-10-23 13:06:55 -0700641 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600642 // Spawn as thread so init can issue commands back to vold without
643 // causing deadlock, usually as a result of prep_data_fs.
644 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600645 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700646 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600647}
648
649binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100650 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600651 ACQUIRE_CRYPT_LOCK;
652
Eric Biggersa701c452018-10-23 13:06:55 -0700653 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600654}
655
656binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100657 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600658 ACQUIRE_CRYPT_LOCK;
659
660 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700661 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600662}
663
Paul Lawrence236e5e82019-06-25 14:44:33 -0700664binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
665 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100666 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800667 ACQUIRE_LOCK;
668
Paul Lawrence236e5e82019-06-25 14:44:33 -0700669 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800670}
671
Paul Lawrence236e5e82019-06-25 14:44:33 -0700672binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
673 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100674 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800675 ACQUIRE_LOCK;
676
Paul Lawrence236e5e82019-06-25 14:44:33 -0700677 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800678}
679
Songchun Fan01c4e912020-03-03 17:58:20 -0800680binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
681 bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100682 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600683 ACQUIRE_CRYPT_LOCK;
684
Eric Biggersa701c452018-10-23 13:06:55 -0700685 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600686}
687
688binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100689 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600690 ACQUIRE_CRYPT_LOCK;
691
Eric Biggersa701c452018-10-23 13:06:55 -0700692 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600693}
694
695binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700696 const std::string& token,
697 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100698 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600699 ACQUIRE_CRYPT_LOCK;
700
Eric Biggersa701c452018-10-23 13:06:55 -0700701 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702}
703
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700704binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
705 const std::string& token,
706 const std::string& secret) {
707 ENFORCE_SYSTEM_OR_ROOT;
708 ACQUIRE_CRYPT_LOCK;
709
710 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
711}
712
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600713binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
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_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600718}
719
720binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700721 const std::string& token,
722 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100723 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724 ACQUIRE_CRYPT_LOCK;
725
Eric Biggersa701c452018-10-23 13:06:55 -0700726 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600727}
728
729binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100730 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600731 ACQUIRE_CRYPT_LOCK;
732
Eric Biggersa701c452018-10-23 13:06:55 -0700733 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600734}
735
Jooyung Han4fcb7072020-01-23 13:23:26 +0900736binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700737 int32_t userId, int32_t userSerial,
738 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100739 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700740 std::string empty_string = "";
741 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700742 CHECK_ARGUMENT_HEX(uuid_);
743
744 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700745 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600746}
747
Jooyung Han4fcb7072020-01-23 13:23:26 +0900748binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700749 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100750 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700751 std::string empty_string = "";
752 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700753 CHECK_ARGUMENT_HEX(uuid_);
754
755 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700756 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600757}
758
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700759binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
760 int32_t appId, const std::string& sandboxId,
761 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700762 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700763}
764
765binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700766 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700767 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700768 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700769}
770
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700771binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100772 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700773 ACQUIRE_LOCK;
774
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700775 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700776}
777
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700778binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100779 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700780 ACQUIRE_LOCK;
781
782 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700783 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700784}
785
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700786binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100787 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700788 ACQUIRE_LOCK;
789
790 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700791 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700792}
793
David Anderson23850d32020-06-10 23:51:17 -0700794binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
795 ENFORCE_SYSTEM_OR_ROOT;
796 ACQUIRE_LOCK;
797
798 *_aidl_return = cp_isCheckpointing();
799 return Ok();
800}
801
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700802binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100803 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700804 ACQUIRE_LOCK;
805
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700806 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700807}
808
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700809binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100810 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700811 ACQUIRE_LOCK;
812
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700813 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700814}
815
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700816binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100817 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700818 CHECK_ARGUMENT_PATH(mountPoint);
819 ACQUIRE_LOCK;
820
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700821 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700822}
823
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800824binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100825 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800826 CHECK_ARGUMENT_PATH(mountPoint);
827 ACQUIRE_LOCK;
828
829 return cp_restoreCheckpoint(mountPoint, count);
830}
831
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700832binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100833 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700834 ACQUIRE_LOCK;
835
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700836 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700837}
838
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700839binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100840 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700841 ACQUIRE_LOCK;
842
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700843 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700844 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700845}
846
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800847binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100848 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800849 ACQUIRE_LOCK;
850
851 return cp_supportsCheckpoint(*_aidl_return);
852}
853
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700854binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100855 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700856 ACQUIRE_LOCK;
857
858 return cp_supportsBlockCheckpoint(*_aidl_return);
859}
860
861binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100862 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700863 ACQUIRE_LOCK;
864
865 return cp_supportsFileCheckpoint(*_aidl_return);
866}
867
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100868binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100869 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100870 ACQUIRE_LOCK;
871
872 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700873 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100874}
875
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800876binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700877 ENFORCE_SYSTEM_OR_ROOT;
878
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700879 *_aidl_return = incfs::enabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700880 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800881}
882
883binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800884 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800885 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700886 ENFORCE_SYSTEM_OR_ROOT;
887 CHECK_ARGUMENT_PATH(backingPath);
888 CHECK_ARGUMENT_PATH(targetDir);
889
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700890 auto control = incfs::mount(backingPath, targetDir,
891 {.flags = IncFsMountFlags(flags),
892 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
893 // Mount with read logs disabled.
894 .readLogBufferPages = 0});
895 if (!control) {
896 return translate(-errno);
Songchun Fanab55cec2019-12-02 10:50:12 -0800897 }
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700898 auto fds = control.releaseFds();
899 using android::base::unique_fd;
900 _aidl_return->cmd.reset(unique_fd(fds[CMD].release()));
901 _aidl_return->pendingReads.reset(unique_fd(fds[PENDING_READS].release()));
902 _aidl_return->log.reset(unique_fd(fds[LOGS].release()));
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700903 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800904}
905
906binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700907 ENFORCE_SYSTEM_OR_ROOT;
908 CHECK_ARGUMENT_PATH(dir);
909
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700910 return translate(incfs::unmount(dir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800911}
912
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700913binder::Status VoldNativeService::setIncFsMountOptions(
914 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
915 bool enableReadLogs) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -0700916 ENFORCE_SYSTEM_OR_ROOT;
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700917
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700918 auto incfsControl =
919 incfs::createControl(control.cmd.get(), control.pendingReads.get(), control.log.get());
920 auto cleanupFunc = [](auto incfsControl) {
921 for (auto& fd : incfsControl->releaseFds()) {
922 (void)fd.release();
923 }
924 };
925 auto cleanup =
926 std::unique_ptr<incfs::Control, decltype(cleanupFunc)>(&incfsControl, cleanupFunc);
927 if (auto error = incfs::setOptions(
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700928 incfsControl,
929 {.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
930 .readLogBufferPages = enableReadLogs ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES : 0});
931 error < 0) {
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700932 return binder::Status::fromServiceSpecificError(error);
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700933 }
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700934
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700935 return Ok();
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700936}
937
Songchun Fanab55cec2019-12-02 10:50:12 -0800938binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
939 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700940 ENFORCE_SYSTEM_OR_ROOT;
941 CHECK_ARGUMENT_PATH(sourceDir);
942 CHECK_ARGUMENT_PATH(targetDir);
943
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700944 return translate(incfs::bindMount(sourceDir, targetDir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800945}
946
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600947} // namespace vold
948} // namespace android