blob: 03dee4849cacd4b41d18ebce6785e43e7d424f3e [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"
Martijn Coeneneed957f2020-11-12 10:59:13 +010036#include "Keymaster.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060037#include "MetadataCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080038#include "MoveStorage.h"
39#include "Process.h"
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070040#include "VoldNativeServiceValidation.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080041#include "VoldUtil.h"
42#include "VolumeManager.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070043#include "cryptfs.h"
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -070044#include "incfs.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060045
Jeff Sharkey068c6be2017-09-06 13:47:40 -060046using android::base::StringPrintf;
47using std::endl;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070048using namespace std::literals;
Jeff Sharkey068c6be2017-09-06 13:47:40 -060049
50namespace android {
51namespace vold {
52
53namespace {
54
55constexpr const char* kDump = "android.permission.DUMP";
56
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060057static binder::Status error(const std::string& msg) {
58 PLOG(ERROR) << msg;
59 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
60}
61
Jeff Sharkey83b559c2017-09-12 16:30:52 -060062static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060063 if (status == 0) {
64 return binder::Status::ok();
65 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060066 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060067 }
68}
69
Jeff Sharkey83b559c2017-09-12 16:30:52 -060070static binder::Status translateBool(bool status) {
71 if (status) {
72 return binder::Status::ok();
73 } else {
74 return binder::Status::fromServiceSpecificError(status);
75 }
76}
77
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010078#define ENFORCE_SYSTEM_OR_ROOT \
79 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070080 binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010081 if (!status.isOk()) { \
82 return status; \
83 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070084 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -060085
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070086#define CHECK_ARGUMENT_ID(id) \
87 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070088 binder::Status status = CheckArgumentId((id)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070089 if (!status.isOk()) { \
90 return status; \
91 } \
92 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -060093
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070094#define CHECK_ARGUMENT_PATH(path) \
95 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070096 binder::Status status = CheckArgumentPath((path)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070097 if (!status.isOk()) { \
98 return status; \
99 } \
100 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600101
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102#define CHECK_ARGUMENT_HEX(hex) \
103 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700104 binder::Status status = CheckArgumentHex((hex)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700105 if (!status.isOk()) { \
106 return status; \
107 } \
108 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600109
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700110#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600111 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
112 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600113
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700114#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600115 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
116 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600117
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600118} // namespace
119
120status_t VoldNativeService::start() {
121 IPCThreadState::self()->disableBackgroundScheduling(true);
122 status_t ret = BinderService<VoldNativeService>::publish();
123 if (ret != android::OK) {
124 return ret;
125 }
126 sp<ProcessState> ps(ProcessState::self());
127 ps->startThreadPool();
128 ps->giveThreadPoolName();
129 return android::OK;
130}
131
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700132status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600133 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700134 const binder::Status dump_permission = CheckPermission(kDump);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600135 if (!dump_permission.isOk()) {
136 out << dump_permission.toString8() << endl;
137 return PERMISSION_DENIED;
138 }
139
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600140 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600141 out << "vold is happy!" << endl;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600142 return NO_ERROR;
143}
144
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600145binder::Status VoldNativeService::setListener(
Songchun Fan01c4e912020-03-03 17:58:20 -0800146 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100147 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600148 ACQUIRE_LOCK;
149
150 VolumeManager::Instance()->setListener(listener);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700151 return Ok();
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600152}
153
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600154binder::Status VoldNativeService::monitor() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100155 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600156
157 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700158 { ACQUIRE_LOCK; }
159 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600160
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700161 return Ok();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600162}
163
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600164binder::Status VoldNativeService::reset() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100165 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600166 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600167
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600168 return translate(VolumeManager::Instance()->reset());
169}
170
171binder::Status VoldNativeService::shutdown() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100172 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600173 ACQUIRE_LOCK;
174
175 return translate(VolumeManager::Instance()->shutdown());
176}
177
Martijn Coenen23c04452020-04-29 07:49:41 +0200178binder::Status VoldNativeService::abortFuse() {
179 ENFORCE_SYSTEM_OR_ROOT;
180 ACQUIRE_LOCK;
181
182 return translate(VolumeManager::Instance()->abortFuse());
183}
184
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600185binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100186 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600187 ACQUIRE_LOCK;
188
189 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
190}
191
192binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100193 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600194 ACQUIRE_LOCK;
195
196 return translate(VolumeManager::Instance()->onUserRemoved(userId));
197}
198
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700199binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100200 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600201 ACQUIRE_LOCK;
202
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700203 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600204}
205
206binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100207 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600208 ACQUIRE_LOCK;
209
210 return translate(VolumeManager::Instance()->onUserStopped(userId));
211}
212
Sudheer Shankad484aa92018-07-31 10:07:34 -0700213binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700214 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700215 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700216}
217
Sudheer Shankad484aa92018-07-31 10:07:34 -0700218binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700219 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700220 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700221}
222
Jeff Sharkey401b2602017-12-14 22:15:20 -0700223binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100224 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700225 ACQUIRE_LOCK;
226
227 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
228}
229
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600230binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700231 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100232 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600233 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600234 ACQUIRE_LOCK;
235
236 auto disk = VolumeManager::Instance()->findDisk(diskId);
237 if (disk == nullptr) {
238 return error("Failed to find disk " + diskId);
239 }
240 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700241 case PARTITION_TYPE_PUBLIC:
242 return translate(disk->partitionPublic());
243 case PARTITION_TYPE_PRIVATE:
244 return translate(disk->partitionPrivate());
245 case PARTITION_TYPE_MIXED:
246 return translate(disk->partitionMixed(ratio));
247 default:
248 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600249 }
250}
251
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600252binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700253 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100254 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600255 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600256 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600257 ACQUIRE_LOCK;
258
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600259 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600260}
261
Zim5048b4b2019-11-19 09:16:03 +0000262binder::Status VoldNativeService::mount(
263 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
264 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100265 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600266 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600267 ACQUIRE_LOCK;
268
269 auto vol = VolumeManager::Instance()->findVolume(volId);
270 if (vol == nullptr) {
271 return error("Failed to find volume " + volId);
272 }
273
274 vol->setMountFlags(mountFlags);
275 vol->setMountUserId(mountUserId);
276
Zim5048b4b2019-11-19 09:16:03 +0000277 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600278 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000279 vol->setMountCallback(nullptr);
280
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700281 if (res != OK) {
282 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600283 }
Zim3623a212019-07-19 16:46:53 +0100284
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700285 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600286}
287
288binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100289 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600290 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600291 ACQUIRE_LOCK;
292
293 auto vol = VolumeManager::Instance()->findVolume(volId);
294 if (vol == nullptr) {
295 return error("Failed to find volume " + volId);
296 }
297 return translate(vol->unmount());
298}
299
300binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100301 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600302 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600303 ACQUIRE_LOCK;
304
305 auto vol = VolumeManager::Instance()->findVolume(volId);
306 if (vol == nullptr) {
307 return error("Failed to find volume " + volId);
308 }
309 return translate(vol->format(fsType));
310}
311
Jeff Sharkey2048a282017-06-15 09:59:43 -0600312static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600313 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600314 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600315 } else {
316 auto vol = VolumeManager::Instance()->findVolume(volId);
317 if (vol == nullptr) {
318 return error("Failed to find volume " + volId);
319 }
320 if (vol->getType() != VolumeBase::Type::kPrivate) {
321 return error("Volume " + volId + " not private");
322 }
323 if (vol->getState() != VolumeBase::State::kMounted) {
324 return error("Volume " + volId + " not mounted");
325 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600326 *path = vol->getPath();
327 if (path->empty()) {
328 return error("Volume " + volId + " missing path");
329 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600330 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700331 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600332}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600333
Jeff Sharkey2048a282017-06-15 09:59:43 -0600334binder::Status VoldNativeService::benchmark(
Songchun Fan01c4e912020-03-03 17:58:20 -0800335 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100336 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600337 CHECK_ARGUMENT_ID(volId);
338 ACQUIRE_LOCK;
339
340 std::string path;
341 auto status = pathForVolId(volId, &path);
342 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600343
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700344 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700345 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600346}
347
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700348binder::Status VoldNativeService::moveStorage(
Songchun Fan01c4e912020-03-03 17:58:20 -0800349 const std::string& fromVolId, const std::string& toVolId,
350 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100351 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600352 CHECK_ARGUMENT_ID(fromVolId);
353 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600354 ACQUIRE_LOCK;
355
356 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
357 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
358 if (fromVol == nullptr) {
359 return error("Failed to find volume " + fromVolId);
360 } else if (toVol == nullptr) {
361 return error("Failed to find volume " + toVolId);
362 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600363
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700364 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700365 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600366}
367
368binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100369 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600370 ACQUIRE_LOCK;
371
Sudheer Shanka817b9112018-12-13 17:40:28 -0800372 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600373}
374
Ricky Wai6b122572020-02-28 16:30:47 +0000375binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
376 const std::vector<std::string>& packageNames) {
377 ENFORCE_SYSTEM_OR_ROOT;
378 ACQUIRE_LOCK;
379
380 return translate(VolumeManager::Instance()->remountAppStorageDirs(uid, pid, packageNames));
381}
382
Martijn Coenen8a68a072020-02-12 15:29:02 +0100383binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100384 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600385 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600386 ACQUIRE_LOCK;
387
Martijn Coenen8a68a072020-02-12 15:29:02 +0100388 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600389}
390
Ricky Waibbfb6ea2020-12-03 15:32:52 +0000391binder::Status VoldNativeService::ensureAppDirsCreated(const std::vector<std::string>& paths,
392 int32_t appUid) {
393 ENFORCE_SYSTEM_OR_ROOT;
394 ACQUIRE_LOCK;
395
396 return translate(VolumeManager::Instance()->ensureAppDirsCreated(paths, appUid));
397}
398
Martijn Coenen816f4d92020-02-18 15:06:37 +0100399binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
400 ENFORCE_SYSTEM_OR_ROOT;
401 CHECK_ARGUMENT_PATH(path);
402 ACQUIRE_LOCK;
403
404 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
405}
406
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600407binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700408 const std::string& sourceKey, int32_t ownerGid,
409 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100410 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600411 CHECK_ARGUMENT_PATH(sourcePath);
412 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600413 ACQUIRE_LOCK;
414
415 return translate(
Songchun Fan01c4e912020-03-03 17:58:20 -0800416 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600417}
418
419binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100420 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600421 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600422 ACQUIRE_LOCK;
423
424 return translate(VolumeManager::Instance()->destroyObb(volId));
425}
426
Risan82e90de2020-02-04 16:07:21 +0900427binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
428 const std::string& mountPath,
429 const std::string& fsType,
430 const std::string& fsUuid,
431 const std::string& fsLabel, int32_t flags,
432 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100433 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900434 CHECK_ARGUMENT_PATH(sourcePath);
435 CHECK_ARGUMENT_PATH(mountPath);
436 CHECK_ARGUMENT_HEX(fsUuid);
437 // Label limitation seems to be different between fs (including allowed characters), so checking
438 // is quite meaningless.
439 ACQUIRE_LOCK;
440
Risan82e90de2020-02-04 16:07:21 +0900441 return translate(VolumeManager::Instance()->createStubVolume(
442 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900443}
444
445binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100446 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900447 CHECK_ARGUMENT_ID(volId);
448 ACQUIRE_LOCK;
449
450 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
451}
452
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700453binder::Status VoldNativeService::fstrim(
Songchun Fan01c4e912020-03-03 17:58:20 -0800454 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100455 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600456 ACQUIRE_LOCK;
457
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700458 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700459 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600460}
461
Jin Qiana370c142017-10-17 15:41:45 -0700462binder::Status VoldNativeService::runIdleMaint(
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::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700468 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700469}
470
471binder::Status VoldNativeService::abortIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800472 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100473 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700474 ACQUIRE_LOCK;
475
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700476 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700477 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700478}
479
Risan8f6198d2018-10-26 20:56:45 -0600480binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700481 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100482 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600483 ACQUIRE_LOCK;
484
Risan8f6198d2018-10-26 20:56:45 -0600485 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600486}
487
Risan8f6198d2018-10-26 20:56:45 -0600488binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100489 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600490 ACQUIRE_LOCK;
491
Risan8f6198d2018-10-26 20:56:45 -0600492 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
493}
494
495binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
496 int32_t flags,
497 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100498 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600499 ACQUIRE_LOCK;
500
501 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
502 if (fd == -1) {
503 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
504 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
505 " flags: " + std::to_string(flags));
506 }
507
508 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700509 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600510}
511
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600512binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100513 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600514 ACQUIRE_CRYPT_LOCK;
515
516 return translate(cryptfs_check_passwd(password.c_str()));
517}
518
519binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100520 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600521 ACQUIRE_CRYPT_LOCK;
522
523 // Spawn as thread so init can issue commands back to vold without
524 // causing deadlock, usually as a result of prep_data_fs.
525 std::thread(&cryptfs_restart).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700526 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600527}
528
529binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100530 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600531 ACQUIRE_CRYPT_LOCK;
532
533 *_aidl_return = cryptfs_crypto_complete();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700534 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600535}
536
537static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700538 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600539 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
540
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600541 for (int tries = 0; tries < 2; ++tries) {
542 int rc;
543 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800544 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600545 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800546 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600547 }
548
549 if (rc == 0) {
550 return 0;
551 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600552 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600553 }
554 }
555
556 return -1;
557}
558
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700559binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
560 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100561 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600562 ACQUIRE_CRYPT_LOCK;
563
Paul Crowley0fd26262018-01-30 09:48:19 -0800564 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700565 if (fscrypt_is_native()) {
566 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
567 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600568 }
Eric Biggersa701c452018-10-23 13:06:55 -0700569 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600570
571 // Spawn as thread so init can issue commands back to vold without
572 // causing deadlock, usually as a result of prep_data_fs.
573 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700574 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600575}
576
577binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700578 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100579 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600580 ACQUIRE_CRYPT_LOCK;
581
582 return translate(cryptfs_changepw(passwordType, password.c_str()));
583}
584
585binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100586 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600587 ACQUIRE_CRYPT_LOCK;
588
589 return translate(cryptfs_verify_passwd(password.c_str()));
590}
591
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700592binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100593 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600594 ACQUIRE_CRYPT_LOCK;
595
596 char buf[PROPERTY_VALUE_MAX];
597 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
598 return error(StringPrintf("Failed to read field %s", key.c_str()));
599 } else {
600 *_aidl_return = buf;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700601 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600602 }
603}
604
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700605binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100606 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600607 ACQUIRE_CRYPT_LOCK;
608
609 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
610}
611
612binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100613 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600614 ACQUIRE_CRYPT_LOCK;
615
616 *_aidl_return = cryptfs_get_password_type();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700617 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600618}
619
620binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100621 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600622 ACQUIRE_CRYPT_LOCK;
623
624 const char* res = cryptfs_get_password();
625 if (res != nullptr) {
626 *_aidl_return = res;
627 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700628 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600629}
630
631binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100632 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600633 ACQUIRE_CRYPT_LOCK;
634
635 cryptfs_clear_password();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700636 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600637}
638
639binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100640 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600641 ACQUIRE_CRYPT_LOCK;
642
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700643 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600644}
645
646binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100647 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648 ACQUIRE_CRYPT_LOCK;
649
Eric Biggersa701c452018-10-23 13:06:55 -0700650 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600651 // Spawn as thread so init can issue commands back to vold without
652 // causing deadlock, usually as a result of prep_data_fs.
653 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600654 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700655 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600656}
657
658binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100659 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600660 ACQUIRE_CRYPT_LOCK;
661
Eric Biggersa701c452018-10-23 13:06:55 -0700662 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600663}
664
665binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100666 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600667 ACQUIRE_CRYPT_LOCK;
668
669 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700670 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600671}
672
Paul Lawrence236e5e82019-06-25 14:44:33 -0700673binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
674 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100675 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800676 ACQUIRE_LOCK;
677
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800678 return translateBool(
679 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false, "null"));
Paul Crowley0fd26262018-01-30 09:48:19 -0800680}
681
Paul Lawrence236e5e82019-06-25 14:44:33 -0700682binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800683 const std::string& mountPoint, bool shouldFormat,
684 const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100685 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800686 ACQUIRE_LOCK;
687
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800688 return translateBool(
689 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat, fsType));
Paul Crowley0fd26262018-01-30 09:48:19 -0800690}
691
Songchun Fan01c4e912020-03-03 17:58:20 -0800692binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
693 bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100694 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600695 ACQUIRE_CRYPT_LOCK;
696
Eric Biggersa701c452018-10-23 13:06:55 -0700697 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600698}
699
700binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100701 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600702 ACQUIRE_CRYPT_LOCK;
703
Eric Biggersa701c452018-10-23 13:06:55 -0700704 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705}
706
707binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700708 const std::string& token,
709 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100710 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600711 ACQUIRE_CRYPT_LOCK;
712
Eric Biggersa701c452018-10-23 13:06:55 -0700713 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600714}
715
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700716binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
717 const std::string& token,
718 const std::string& secret) {
719 ENFORCE_SYSTEM_OR_ROOT;
720 ACQUIRE_CRYPT_LOCK;
721
722 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, token, secret));
723}
724
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600725binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100726 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600727 ACQUIRE_CRYPT_LOCK;
728
Eric Biggersa701c452018-10-23 13:06:55 -0700729 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600730}
731
732binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700733 const std::string& token,
734 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100735 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600736 ACQUIRE_CRYPT_LOCK;
737
Eric Biggersa701c452018-10-23 13:06:55 -0700738 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600739}
740
741binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100742 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600743 ACQUIRE_CRYPT_LOCK;
744
Eric Biggersa701c452018-10-23 13:06:55 -0700745 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600746}
747
Jooyung Han4fcb7072020-01-23 13:23:26 +0900748binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700749 int32_t userId, int32_t userSerial,
750 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100751 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700752 std::string empty_string = "";
753 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700754 CHECK_ARGUMENT_HEX(uuid_);
755
756 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700757 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600758}
759
Jooyung Han4fcb7072020-01-23 13:23:26 +0900760binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700761 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100762 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700763 std::string empty_string = "";
764 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700765 CHECK_ARGUMENT_HEX(uuid_);
766
767 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700768 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600769}
770
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700771binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
772 int32_t appId, const std::string& sandboxId,
773 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700774 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700775}
776
777binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700778 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700779 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700780 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700781}
782
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700783binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100784 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700785 ACQUIRE_LOCK;
786
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700787 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700788}
789
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700790binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100791 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700792 ACQUIRE_LOCK;
793
794 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700795 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700796}
797
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700798binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100799 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700800 ACQUIRE_LOCK;
801
802 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700803 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700804}
805
David Anderson23850d32020-06-10 23:51:17 -0700806binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
807 ENFORCE_SYSTEM_OR_ROOT;
808 ACQUIRE_LOCK;
809
810 *_aidl_return = cp_isCheckpointing();
811 return Ok();
812}
813
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700814binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100815 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700816 ACQUIRE_LOCK;
817
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700818 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700819}
820
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700821binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100822 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700823 ACQUIRE_LOCK;
824
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700825 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700826}
827
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700828binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100829 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700830 CHECK_ARGUMENT_PATH(mountPoint);
831 ACQUIRE_LOCK;
832
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700833 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700834}
835
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800836binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100837 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800838 CHECK_ARGUMENT_PATH(mountPoint);
839 ACQUIRE_LOCK;
840
841 return cp_restoreCheckpoint(mountPoint, count);
842}
843
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700844binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100845 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700846 ACQUIRE_LOCK;
847
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700848 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700849}
850
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700851binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100852 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700853 ACQUIRE_LOCK;
854
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700855 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700856 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700857}
858
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800859binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100860 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800861 ACQUIRE_LOCK;
862
863 return cp_supportsCheckpoint(*_aidl_return);
864}
865
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700866binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100867 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700868 ACQUIRE_LOCK;
869
870 return cp_supportsBlockCheckpoint(*_aidl_return);
871}
872
873binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100874 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700875 ACQUIRE_LOCK;
876
877 return cp_supportsFileCheckpoint(*_aidl_return);
878}
879
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100880binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100881 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100882 ACQUIRE_LOCK;
883
884 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700885 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100886}
887
Martijn Coeneneed957f2020-11-12 10:59:13 +0100888binder::Status VoldNativeService::earlyBootEnded() {
889 ENFORCE_SYSTEM_OR_ROOT;
890 ACQUIRE_LOCK;
891
892 Keymaster::earlyBootEnded();
893 return Ok();
894}
895
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800896binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700897 ENFORCE_SYSTEM_OR_ROOT;
898
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700899 *_aidl_return = incfs::enabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700900 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800901}
902
903binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800904 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fanab55cec2019-12-02 10:50:12 -0800905 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700906 ENFORCE_SYSTEM_OR_ROOT;
907 CHECK_ARGUMENT_PATH(backingPath);
908 CHECK_ARGUMENT_PATH(targetDir);
909
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700910 auto control = incfs::mount(backingPath, targetDir,
911 {.flags = IncFsMountFlags(flags),
912 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
913 // Mount with read logs disabled.
914 .readLogBufferPages = 0});
915 if (!control) {
916 return translate(-errno);
Songchun Fanab55cec2019-12-02 10:50:12 -0800917 }
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700918 auto fds = control.releaseFds();
919 using android::base::unique_fd;
920 _aidl_return->cmd.reset(unique_fd(fds[CMD].release()));
921 _aidl_return->pendingReads.reset(unique_fd(fds[PENDING_READS].release()));
922 _aidl_return->log.reset(unique_fd(fds[LOGS].release()));
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700923 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800924}
925
926binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700927 ENFORCE_SYSTEM_OR_ROOT;
928 CHECK_ARGUMENT_PATH(dir);
929
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700930 return translate(incfs::unmount(dir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800931}
932
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700933binder::Status VoldNativeService::setIncFsMountOptions(
934 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
935 bool enableReadLogs) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -0700936 ENFORCE_SYSTEM_OR_ROOT;
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700937
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700938 auto incfsControl =
939 incfs::createControl(control.cmd.get(), control.pendingReads.get(), control.log.get());
940 auto cleanupFunc = [](auto incfsControl) {
941 for (auto& fd : incfsControl->releaseFds()) {
942 (void)fd.release();
943 }
944 };
945 auto cleanup =
946 std::unique_ptr<incfs::Control, decltype(cleanupFunc)>(&incfsControl, cleanupFunc);
947 if (auto error = incfs::setOptions(
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700948 incfsControl,
949 {.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
950 .readLogBufferPages = enableReadLogs ? INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES : 0});
951 error < 0) {
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700952 return binder::Status::fromServiceSpecificError(error);
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700953 }
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700954
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700955 return Ok();
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -0700956}
957
Songchun Fanab55cec2019-12-02 10:50:12 -0800958binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
959 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700960 ENFORCE_SYSTEM_OR_ROOT;
961 CHECK_ARGUMENT_PATH(sourceDir);
962 CHECK_ARGUMENT_PATH(targetDir);
963
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700964 return translate(incfs::bindMount(sourceDir, targetDir));
Songchun Fanab55cec2019-12-02 10:50:12 -0800965}
966
Yo Chiang0af25a32020-10-07 14:20:00 +0800967binder::Status VoldNativeService::destroyDsuMetadataKey(const std::string& dsuSlot) {
968 ENFORCE_SYSTEM_OR_ROOT;
969 ACQUIRE_LOCK;
970
971 return translateBool(destroy_dsu_metadata_key(dsuSlot));
972}
973
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600974} // namespace vold
975} // namespace android