blob: 1f7638fede569ec10cc7be1fb6d8dcf39e09af04 [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"
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060020#include "Benchmark.h"
Jeff Sharkey2048a282017-06-15 09:59:43 -060021#include "CheckEncryption.h"
22#include "IdleMaint.h"
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060023#include "MoveStorage.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060024#include "Process.h"
Jeff Sharkey2048a282017-06-15 09:59:43 -060025#include "VolumeManager.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060026
Daniel Rosenberg4f684712018-08-28 01:58:49 -070027#include "Checkpoint.h"
Eric Biggersa701c452018-10-23 13:06:55 -070028#include "FsCrypt.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060029#include "MetadataCrypt.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070030#include "cryptfs.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060031
Jeff Sharkey068c6be2017-09-06 13:47:40 -060032#include <fstream>
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060033#include <thread>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060034
35#include <android-base/logging.h>
36#include <android-base/stringprintf.h>
37#include <android-base/strings.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060038#include <fs_mgr.h>
Eric Biggersa701c452018-10-23 13:06:55 -070039#include <fscrypt/fscrypt.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060040#include <private/android_filesystem_config.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060041#include <utils/Trace.h>
Jeff Sharkey068c6be2017-09-06 13:47:40 -060042
Jeff Sharkey068c6be2017-09-06 13:47:40 -060043using android::base::StringPrintf;
44using std::endl;
45
46namespace android {
47namespace vold {
48
49namespace {
50
51constexpr const char* kDump = "android.permission.DUMP";
52
53static binder::Status ok() {
54 return binder::Status::ok();
55}
56
57static binder::Status exception(uint32_t code, const std::string& msg) {
58 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
59}
60
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060061static binder::Status error(const std::string& msg) {
62 PLOG(ERROR) << msg;
63 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
64}
65
Jeff Sharkey83b559c2017-09-12 16:30:52 -060066static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060067 if (status == 0) {
68 return binder::Status::ok();
69 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060070 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060071 }
72}
73
Jeff Sharkey83b559c2017-09-12 16:30:52 -060074static binder::Status translateBool(bool status) {
75 if (status) {
76 return binder::Status::ok();
77 } else {
78 return binder::Status::fromServiceSpecificError(status);
79 }
80}
81
Jeff Sharkey068c6be2017-09-06 13:47:40 -060082binder::Status checkPermission(const char* permission) {
83 pid_t pid;
84 uid_t uid;
85
86 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070087 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060088 return ok();
89 } else {
90 return exception(binder::Status::EX_SECURITY,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070091 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey068c6be2017-09-06 13:47:40 -060092 }
93}
94
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010095binder::Status checkUidOrRoot(uid_t expectedUid) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -060096 uid_t uid = IPCThreadState::self()->getCallingUid();
97 if (uid == expectedUid || uid == AID_ROOT) {
98 return ok();
99 } else {
100 return exception(binder::Status::EX_SECURITY,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700101 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600102 }
103}
104
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600105binder::Status checkArgumentId(const std::string& id) {
106 if (id.empty()) {
107 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
108 }
109 for (const char& c : id) {
Zima438b242019-09-25 14:37:38 +0100110 if (!std::isalnum(c) && c != ':' && c != ',' && c != ';') {
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600111 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700112 StringPrintf("ID %s is malformed", id.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600113 }
114 }
115 return ok();
116}
117
118binder::Status checkArgumentPath(const std::string& path) {
119 if (path.empty()) {
120 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
121 }
122 if (path[0] != '/') {
123 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700124 StringPrintf("Path %s is relative", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600125 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600126 if ((path + '/').find("/../") != std::string::npos) {
127 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700128 StringPrintf("Path %s is shady", path.c_str()));
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600129 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600130 for (const char& c : path) {
131 if (c == '\0' || c == '\n') {
132 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700133 StringPrintf("Path %s is malformed", path.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600134 }
135 }
136 return ok();
137}
138
139binder::Status checkArgumentHex(const std::string& hex) {
140 // Empty hex strings are allowed
141 for (const char& c : hex) {
142 if (!std::isxdigit(c) && c != ':' && c != '-') {
143 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700144 StringPrintf("Hex %s is malformed", hex.c_str()));
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600145 }
146 }
147 return ok();
148}
149
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100150#define ENFORCE_SYSTEM_OR_ROOT \
151 { \
152 binder::Status status = checkUidOrRoot(AID_SYSTEM); \
153 if (!status.isOk()) { \
154 return status; \
155 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700156 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600157
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700158#define CHECK_ARGUMENT_ID(id) \
159 { \
160 binder::Status status = checkArgumentId((id)); \
161 if (!status.isOk()) { \
162 return status; \
163 } \
164 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600165
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700166#define CHECK_ARGUMENT_PATH(path) \
167 { \
168 binder::Status status = checkArgumentPath((path)); \
169 if (!status.isOk()) { \
170 return status; \
171 } \
172 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600173
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700174#define CHECK_ARGUMENT_HEX(hex) \
175 { \
176 binder::Status status = checkArgumentHex((hex)); \
177 if (!status.isOk()) { \
178 return status; \
179 } \
180 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600181
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700182#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600183 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
184 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600185
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700186#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600187 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
188 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600189
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600190} // namespace
191
192status_t VoldNativeService::start() {
193 IPCThreadState::self()->disableBackgroundScheduling(true);
194 status_t ret = BinderService<VoldNativeService>::publish();
195 if (ret != android::OK) {
196 return ret;
197 }
198 sp<ProcessState> ps(ProcessState::self());
199 ps->startThreadPool();
200 ps->giveThreadPoolName();
201 return android::OK;
202}
203
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700204status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600205 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
206 const binder::Status dump_permission = checkPermission(kDump);
207 if (!dump_permission.isOk()) {
208 out << dump_permission.toString8() << endl;
209 return PERMISSION_DENIED;
210 }
211
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600212 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600213 out << "vold is happy!" << endl;
214 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600215 return NO_ERROR;
216}
217
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600218binder::Status VoldNativeService::setListener(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700219 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100220 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600221 ACQUIRE_LOCK;
222
223 VolumeManager::Instance()->setListener(listener);
224 return ok();
225}
226
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600227binder::Status VoldNativeService::monitor() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100228 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600229
230 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700231 { ACQUIRE_LOCK; }
232 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600233
234 return ok();
235}
236
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600237binder::Status VoldNativeService::reset() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100238 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600239 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600240
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600241 return translate(VolumeManager::Instance()->reset());
242}
243
244binder::Status VoldNativeService::shutdown() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100245 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600246 ACQUIRE_LOCK;
247
248 return translate(VolumeManager::Instance()->shutdown());
249}
250
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600251binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100252 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600253 ACQUIRE_LOCK;
254
255 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
256}
257
258binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100259 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600260 ACQUIRE_LOCK;
261
262 return translate(VolumeManager::Instance()->onUserRemoved(userId));
263}
264
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700265binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100266 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600267 ACQUIRE_LOCK;
268
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700269 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600270}
271
272binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100273 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600274 ACQUIRE_LOCK;
275
276 return translate(VolumeManager::Instance()->onUserStopped(userId));
277}
278
Sudheer Shankad484aa92018-07-31 10:07:34 -0700279binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700280 const std::vector<int32_t>& appIds) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700281 return ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700282}
283
Sudheer Shankad484aa92018-07-31 10:07:34 -0700284binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700285 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700286 return ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700287}
288
Jeff Sharkey401b2602017-12-14 22:15:20 -0700289binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100290 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700291 ACQUIRE_LOCK;
292
293 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
294}
295
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600296binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700297 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100298 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600299 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600300 ACQUIRE_LOCK;
301
302 auto disk = VolumeManager::Instance()->findDisk(diskId);
303 if (disk == nullptr) {
304 return error("Failed to find disk " + diskId);
305 }
306 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700307 case PARTITION_TYPE_PUBLIC:
308 return translate(disk->partitionPublic());
309 case PARTITION_TYPE_PRIVATE:
310 return translate(disk->partitionPrivate());
311 case PARTITION_TYPE_MIXED:
312 return translate(disk->partitionMixed(ratio));
313 default:
314 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600315 }
316}
317
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600318binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700319 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100320 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600321 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600322 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600323 ACQUIRE_LOCK;
324
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600325 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600326}
327
Zim5048b4b2019-11-19 09:16:03 +0000328binder::Status VoldNativeService::mount(
329 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
330 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100331 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600332 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600333 ACQUIRE_LOCK;
334
335 auto vol = VolumeManager::Instance()->findVolume(volId);
336 if (vol == nullptr) {
337 return error("Failed to find volume " + volId);
338 }
339
340 vol->setMountFlags(mountFlags);
341 vol->setMountUserId(mountUserId);
342
Zim5048b4b2019-11-19 09:16:03 +0000343 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600344 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000345 vol->setMountCallback(nullptr);
346
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700347 if (res != OK) {
348 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600349 }
Zim3623a212019-07-19 16:46:53 +0100350
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700351 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
352 res = VolumeManager::Instance()->setPrimary(vol);
353 if (res != OK) {
354 return translate(res);
355 }
356 }
357 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600358}
359
360binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100361 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600362 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600363 ACQUIRE_LOCK;
364
365 auto vol = VolumeManager::Instance()->findVolume(volId);
366 if (vol == nullptr) {
367 return error("Failed to find volume " + volId);
368 }
369 return translate(vol->unmount());
370}
371
372binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100373 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600374 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600375 ACQUIRE_LOCK;
376
377 auto vol = VolumeManager::Instance()->findVolume(volId);
378 if (vol == nullptr) {
379 return error("Failed to find volume " + volId);
380 }
381 return translate(vol->format(fsType));
382}
383
Jeff Sharkey2048a282017-06-15 09:59:43 -0600384static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600385 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600386 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600387 } else {
388 auto vol = VolumeManager::Instance()->findVolume(volId);
389 if (vol == nullptr) {
390 return error("Failed to find volume " + volId);
391 }
392 if (vol->getType() != VolumeBase::Type::kPrivate) {
393 return error("Volume " + volId + " not private");
394 }
395 if (vol->getState() != VolumeBase::State::kMounted) {
396 return error("Volume " + volId + " not mounted");
397 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600398 *path = vol->getPath();
399 if (path->empty()) {
400 return error("Volume " + volId + " missing path");
401 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600402 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600403 return ok();
404}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600405
Jeff Sharkey2048a282017-06-15 09:59:43 -0600406binder::Status VoldNativeService::benchmark(
407 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100408 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600409 CHECK_ARGUMENT_ID(volId);
410 ACQUIRE_LOCK;
411
412 std::string path;
413 auto status = pathForVolId(volId, &path);
414 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600415
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700416 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600417 return ok();
418}
419
Jeff Sharkey2048a282017-06-15 09:59:43 -0600420binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100421 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600422 CHECK_ARGUMENT_ID(volId);
423 ACQUIRE_LOCK;
424
425 std::string path;
426 auto status = pathForVolId(volId, &path);
427 if (!status.isOk()) return status;
428 return translate(android::vold::CheckEncryption(path));
429}
430
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700431binder::Status VoldNativeService::moveStorage(
432 const std::string& fromVolId, const std::string& toVolId,
433 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100434 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600435 CHECK_ARGUMENT_ID(fromVolId);
436 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600437 ACQUIRE_LOCK;
438
439 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
440 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
441 if (fromVol == nullptr) {
442 return error("Failed to find volume " + fromVolId);
443 } else if (toVol == nullptr) {
444 return error("Failed to find volume " + toVolId);
445 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600446
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700447 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600448 return ok();
449}
450
451binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100452 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600453 ACQUIRE_LOCK;
454
Sudheer Shanka817b9112018-12-13 17:40:28 -0800455 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600456}
457
458binder::Status VoldNativeService::mkdirs(const std::string& path) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100459 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600460 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600461 ACQUIRE_LOCK;
462
Jeff Sharkey3472e522017-10-06 18:02:53 -0600463 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600464}
465
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600466binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700467 const std::string& sourceKey, int32_t ownerGid,
468 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100469 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600470 CHECK_ARGUMENT_PATH(sourcePath);
471 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600472 ACQUIRE_LOCK;
473
474 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700475 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600476}
477
478binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100479 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600480 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600481 ACQUIRE_LOCK;
482
483 return translate(VolumeManager::Instance()->destroyObb(volId));
484}
485
Risan8c9f3322018-10-29 08:52:56 +0900486binder::Status VoldNativeService::createStubVolume(
487 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
488 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100489 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900490 CHECK_ARGUMENT_PATH(sourcePath);
491 CHECK_ARGUMENT_PATH(mountPath);
492 CHECK_ARGUMENT_HEX(fsUuid);
493 // Label limitation seems to be different between fs (including allowed characters), so checking
494 // is quite meaningless.
495 ACQUIRE_LOCK;
496
497 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
498 fsUuid, fsLabel, _aidl_return));
499}
500
501binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100502 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900503 CHECK_ARGUMENT_ID(volId);
504 ACQUIRE_LOCK;
505
506 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
507}
508
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700509binder::Status VoldNativeService::fstrim(
510 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100511 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600512 ACQUIRE_LOCK;
513
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700514 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600515 return ok();
516}
517
Jin Qiana370c142017-10-17 15:41:45 -0700518binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700519 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100520 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700521 ACQUIRE_LOCK;
522
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700523 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700524 return ok();
525}
526
527binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700528 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100529 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700530 ACQUIRE_LOCK;
531
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700532 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700533 return ok();
534}
535
Risan8f6198d2018-10-26 20:56:45 -0600536binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700537 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100538 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600539 ACQUIRE_LOCK;
540
Risan8f6198d2018-10-26 20:56:45 -0600541 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600542}
543
Risan8f6198d2018-10-26 20:56:45 -0600544binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100545 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600546 ACQUIRE_LOCK;
547
Risan8f6198d2018-10-26 20:56:45 -0600548 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
549}
550
551binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
552 int32_t flags,
553 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100554 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600555 ACQUIRE_LOCK;
556
557 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
558 if (fd == -1) {
559 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
560 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
561 " flags: " + std::to_string(flags));
562 }
563
564 *_aidl_return = android::base::unique_fd(fd);
565 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600566}
567
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600568binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100569 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600570 ACQUIRE_CRYPT_LOCK;
571
572 return translate(cryptfs_check_passwd(password.c_str()));
573}
574
575binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100576 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600577 ACQUIRE_CRYPT_LOCK;
578
579 // Spawn as thread so init can issue commands back to vold without
580 // causing deadlock, usually as a result of prep_data_fs.
581 std::thread(&cryptfs_restart).detach();
582 return ok();
583}
584
585binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
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 *_aidl_return = cryptfs_crypto_complete();
590 return ok();
591}
592
593static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700594 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600595 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
596
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600597 for (int tries = 0; tries < 2; ++tries) {
598 int rc;
599 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800600 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600601 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800602 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600603 }
604
605 if (rc == 0) {
606 return 0;
607 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600608 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600609 }
610 }
611
612 return -1;
613}
614
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700615binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
616 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100617 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600618 ACQUIRE_CRYPT_LOCK;
619
Paul Crowley0fd26262018-01-30 09:48:19 -0800620 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700621 if (fscrypt_is_native()) {
622 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
623 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600624 }
Eric Biggersa701c452018-10-23 13:06:55 -0700625 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600626
627 // Spawn as thread so init can issue commands back to vold without
628 // causing deadlock, usually as a result of prep_data_fs.
629 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
630 return ok();
631}
632
633binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700634 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100635 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600636 ACQUIRE_CRYPT_LOCK;
637
638 return translate(cryptfs_changepw(passwordType, password.c_str()));
639}
640
641binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100642 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600643 ACQUIRE_CRYPT_LOCK;
644
645 return translate(cryptfs_verify_passwd(password.c_str()));
646}
647
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700648binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100649 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600650 ACQUIRE_CRYPT_LOCK;
651
652 char buf[PROPERTY_VALUE_MAX];
653 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
654 return error(StringPrintf("Failed to read field %s", key.c_str()));
655 } else {
656 *_aidl_return = buf;
657 return ok();
658 }
659}
660
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700661binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100662 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600663 ACQUIRE_CRYPT_LOCK;
664
665 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
666}
667
668binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100669 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600670 ACQUIRE_CRYPT_LOCK;
671
672 *_aidl_return = cryptfs_get_password_type();
673 return ok();
674}
675
676binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100677 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600678 ACQUIRE_CRYPT_LOCK;
679
680 const char* res = cryptfs_get_password();
681 if (res != nullptr) {
682 *_aidl_return = res;
683 }
684 return ok();
685}
686
687binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100688 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600689 ACQUIRE_CRYPT_LOCK;
690
691 cryptfs_clear_password();
692 return ok();
693}
694
695binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100696 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600697 ACQUIRE_CRYPT_LOCK;
698
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700699 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600700}
701
702binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100703 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600704 ACQUIRE_CRYPT_LOCK;
705
Eric Biggersa701c452018-10-23 13:06:55 -0700706 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600707 // Spawn as thread so init can issue commands back to vold without
708 // causing deadlock, usually as a result of prep_data_fs.
709 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600710 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800711 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600712}
713
714binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100715 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600716 ACQUIRE_CRYPT_LOCK;
717
Eric Biggersa701c452018-10-23 13:06:55 -0700718 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600719}
720
721binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100722 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600723 ACQUIRE_CRYPT_LOCK;
724
725 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
726 return ok();
727}
728
Paul Lawrence236e5e82019-06-25 14:44:33 -0700729binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
730 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100731 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800732 ACQUIRE_LOCK;
733
Paul Lawrence236e5e82019-06-25 14:44:33 -0700734 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800735}
736
Paul Lawrence236e5e82019-06-25 14:44:33 -0700737binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
738 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100739 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800740 ACQUIRE_LOCK;
741
Paul Lawrence236e5e82019-06-25 14:44:33 -0700742 return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800743}
744
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700745binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100746 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600747 ACQUIRE_CRYPT_LOCK;
748
Eric Biggersa701c452018-10-23 13:06:55 -0700749 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600750}
751
752binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100753 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600754 ACQUIRE_CRYPT_LOCK;
755
Eric Biggersa701c452018-10-23 13:06:55 -0700756 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600757}
758
759binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700760 const std::string& token,
761 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100762 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600763 ACQUIRE_CRYPT_LOCK;
764
Eric Biggersa701c452018-10-23 13:06:55 -0700765 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600766}
767
768binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100769 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600770 ACQUIRE_CRYPT_LOCK;
771
Eric Biggersa701c452018-10-23 13:06:55 -0700772 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600773}
774
775binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700776 const std::string& token,
777 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100778 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600779 ACQUIRE_CRYPT_LOCK;
780
Eric Biggersa701c452018-10-23 13:06:55 -0700781 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600782}
783
784binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100785 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600786 ACQUIRE_CRYPT_LOCK;
787
Eric Biggersa701c452018-10-23 13:06:55 -0700788 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600789}
790
791binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700792 int32_t userId, int32_t userSerial,
793 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100794 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700795 std::string empty_string = "";
796 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700797 CHECK_ARGUMENT_HEX(uuid_);
798
799 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700800 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600801}
802
803binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700804 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100805 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700806 std::string empty_string = "";
807 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700808 CHECK_ARGUMENT_HEX(uuid_);
809
810 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700811 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600812}
813
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700814binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
815 int32_t appId, const std::string& sandboxId,
816 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700817 return ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700818}
819
820binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700821 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700822 int32_t userId) {
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700823 return ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700824}
825
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700826binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100827 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700828 ACQUIRE_LOCK;
829
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700830 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700831}
832
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700833binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100834 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700835 ACQUIRE_LOCK;
836
837 *_aidl_return = cp_needsRollback();
838 return ok();
839}
840
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700841binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100842 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700843 ACQUIRE_LOCK;
844
845 *_aidl_return = cp_needsCheckpoint();
846 return ok();
847}
848
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700849binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100850 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700851 ACQUIRE_LOCK;
852
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700853 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700854}
855
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700856binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100857 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700858 ACQUIRE_LOCK;
859
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700860 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700861}
862
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700863binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100864 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700865 CHECK_ARGUMENT_PATH(mountPoint);
866 ACQUIRE_LOCK;
867
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700868 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700869}
870
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800871binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100872 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800873 CHECK_ARGUMENT_PATH(mountPoint);
874 ACQUIRE_LOCK;
875
876 return cp_restoreCheckpoint(mountPoint, count);
877}
878
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700879binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100880 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700881 ACQUIRE_LOCK;
882
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700883 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700884}
885
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700886binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100887 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700888 ACQUIRE_LOCK;
889
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700890 cp_abortChanges(message, retry);
891 return ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700892}
893
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800894binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100895 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800896 ACQUIRE_LOCK;
897
898 return cp_supportsCheckpoint(*_aidl_return);
899}
900
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700901binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100902 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700903 ACQUIRE_LOCK;
904
905 return cp_supportsBlockCheckpoint(*_aidl_return);
906}
907
908binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100909 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700910 ACQUIRE_LOCK;
911
912 return cp_supportsFileCheckpoint(*_aidl_return);
913}
914
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100915binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100916 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100917 ACQUIRE_LOCK;
918
919 cp_resetCheckpoint();
920 return ok();
921}
922
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600923} // namespace vold
924} // namespace android