blob: c2d4e0b19a58b8fb5c820e43634bd49836ec364a [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
95binder::Status checkUid(uid_t expectedUid) {
96 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) {
110 if (!std::isalnum(c) && c != ':' && c != ',') {
111 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
Sudheer Shankacc0df592018-08-02 10:21:42 -0700150binder::Status checkArgumentPackageName(const std::string& packageName) {
151 // This logic is borrowed from PackageParser.java
152 bool hasSep = false;
153 bool front = true;
154
155 for (size_t i = 0; i < packageName.length(); ++i) {
156 char c = packageName[i];
157 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
158 front = false;
159 continue;
160 }
161 if (!front) {
162 if ((c >= '0' && c <= '9') || c == '_') {
163 continue;
164 }
165 }
166 if (c == '.') {
167 hasSep = true;
168 front = true;
169 continue;
170 }
171 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700172 StringPrintf("Bad package character %c in %s", c, packageName.c_str()));
Sudheer Shankacc0df592018-08-02 10:21:42 -0700173 }
174
175 if (front) {
176 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700177 StringPrintf("Missing separator in %s", packageName.c_str()));
Sudheer Shankacc0df592018-08-02 10:21:42 -0700178 }
179
180 return ok();
181}
182
183binder::Status checkArgumentPackageNames(const std::vector<std::string>& packageNames) {
184 for (size_t i = 0; i < packageNames.size(); ++i) {
185 binder::Status status = checkArgumentPackageName(packageNames[i]);
186 if (!status.isOk()) {
187 return status;
188 }
189 }
190 return ok();
191}
192
193binder::Status checkArgumentSandboxId(const std::string& sandboxId) {
Sudheer Shanka637457d2018-11-13 17:41:45 -0800194 // sandboxId will be in either the format shared-<shared-user-id> or <package-name>
Sudheer Shankacc0df592018-08-02 10:21:42 -0700195 // and <shared-user-id> name has same requirements as <package-name>.
196 std::size_t nameStartIndex = 0;
Sudheer Shanka637457d2018-11-13 17:41:45 -0800197 if (android::base::StartsWith(sandboxId, "shared-")) {
198 nameStartIndex = 7; // len("shared-")
Sudheer Shankacc0df592018-08-02 10:21:42 -0700199 }
200 return checkArgumentPackageName(sandboxId.substr(nameStartIndex));
201}
202
203binder::Status checkArgumentSandboxIds(const std::vector<std::string>& sandboxIds) {
204 for (size_t i = 0; i < sandboxIds.size(); ++i) {
205 binder::Status status = checkArgumentSandboxId(sandboxIds[i]);
206 if (!status.isOk()) {
207 return status;
208 }
209 }
210 return ok();
211}
212
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700213#define ENFORCE_UID(uid) \
214 { \
215 binder::Status status = checkUid((uid)); \
216 if (!status.isOk()) { \
217 return status; \
218 } \
219 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600220
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700221#define CHECK_ARGUMENT_ID(id) \
222 { \
223 binder::Status status = checkArgumentId((id)); \
224 if (!status.isOk()) { \
225 return status; \
226 } \
227 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600228
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700229#define CHECK_ARGUMENT_PATH(path) \
230 { \
231 binder::Status status = checkArgumentPath((path)); \
232 if (!status.isOk()) { \
233 return status; \
234 } \
235 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600236
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700237#define CHECK_ARGUMENT_HEX(hex) \
238 { \
239 binder::Status status = checkArgumentHex((hex)); \
240 if (!status.isOk()) { \
241 return status; \
242 } \
243 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600244
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700245#define CHECK_ARGUMENT_PACKAGE_NAMES(packageNames) \
246 { \
247 binder::Status status = checkArgumentPackageNames((packageNames)); \
248 if (!status.isOk()) { \
249 return status; \
250 } \
251 }
Sudheer Shankacc0df592018-08-02 10:21:42 -0700252
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700253#define CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds) \
254 { \
255 binder::Status status = checkArgumentSandboxIds((sandboxIds)); \
256 if (!status.isOk()) { \
257 return status; \
258 } \
259 }
Sudheer Shankacc0df592018-08-02 10:21:42 -0700260
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700261#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) \
262 { \
263 binder::Status status = checkArgumentPackageName((packageName)); \
264 if (!status.isOk()) { \
265 return status; \
266 } \
267 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700268
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700269#define CHECK_ARGUMENT_SANDBOX_ID(sandboxId) \
270 { \
271 binder::Status status = checkArgumentSandboxId((sandboxId)); \
272 if (!status.isOk()) { \
273 return status; \
274 } \
275 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700276
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700277#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600278 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
279 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600280
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700281#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600282 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
283 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600284
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600285} // namespace
286
287status_t VoldNativeService::start() {
288 IPCThreadState::self()->disableBackgroundScheduling(true);
289 status_t ret = BinderService<VoldNativeService>::publish();
290 if (ret != android::OK) {
291 return ret;
292 }
293 sp<ProcessState> ps(ProcessState::self());
294 ps->startThreadPool();
295 ps->giveThreadPoolName();
296 return android::OK;
297}
298
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700299status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600300 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
301 const binder::Status dump_permission = checkPermission(kDump);
302 if (!dump_permission.isOk()) {
303 out << dump_permission.toString8() << endl;
304 return PERMISSION_DENIED;
305 }
306
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600307 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600308 out << "vold is happy!" << endl;
309 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600310 return NO_ERROR;
311}
312
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600313binder::Status VoldNativeService::setListener(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700314 const android::sp<android::os::IVoldListener>& listener) {
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600315 ENFORCE_UID(AID_SYSTEM);
316 ACQUIRE_LOCK;
317
318 VolumeManager::Instance()->setListener(listener);
319 return ok();
320}
321
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600322binder::Status VoldNativeService::monitor() {
323 ENFORCE_UID(AID_SYSTEM);
324
325 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700326 { ACQUIRE_LOCK; }
327 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600328
329 return ok();
330}
331
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600332binder::Status VoldNativeService::reset() {
333 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600334 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600335
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600336 return translate(VolumeManager::Instance()->reset());
337}
338
339binder::Status VoldNativeService::shutdown() {
340 ENFORCE_UID(AID_SYSTEM);
341 ACQUIRE_LOCK;
342
343 return translate(VolumeManager::Instance()->shutdown());
344}
345
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600346binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
347 ENFORCE_UID(AID_SYSTEM);
348 ACQUIRE_LOCK;
349
350 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
351}
352
353binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
354 ENFORCE_UID(AID_SYSTEM);
355 ACQUIRE_LOCK;
356
357 return translate(VolumeManager::Instance()->onUserRemoved(userId));
358}
359
Sudheer Shankaebaad1c2018-07-31 16:39:59 -0700360binder::Status VoldNativeService::onUserStarted(int32_t userId,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700361 const std::vector<std::string>& packageNames,
362 const std::vector<int>& appIds,
363 const std::vector<std::string>& sandboxIds) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600364 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700365 CHECK_ARGUMENT_PACKAGE_NAMES(packageNames);
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700366 CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600367 ACQUIRE_LOCK;
368
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700369 return translate(
370 VolumeManager::Instance()->onUserStarted(userId, packageNames, appIds, sandboxIds));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600371}
372
373binder::Status VoldNativeService::onUserStopped(int32_t userId) {
374 ENFORCE_UID(AID_SYSTEM);
375 ACQUIRE_LOCK;
376
377 return translate(VolumeManager::Instance()->onUserStopped(userId));
378}
379
Sudheer Shankad484aa92018-07-31 10:07:34 -0700380binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700381 const std::vector<int32_t>& appIds) {
Sudheer Shankad484aa92018-07-31 10:07:34 -0700382 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700383 CHECK_ARGUMENT_PACKAGE_NAMES(packageNames);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700384 ACQUIRE_LOCK;
385
386 return translate(VolumeManager::Instance()->addAppIds(packageNames, appIds));
387}
388
Sudheer Shankad484aa92018-07-31 10:07:34 -0700389binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700390 const std::vector<std::string>& sandboxIds) {
Sudheer Shankad484aa92018-07-31 10:07:34 -0700391 ENFORCE_UID(AID_SYSTEM);
Sudheer Shankacc0df592018-08-02 10:21:42 -0700392 CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700393 ACQUIRE_LOCK;
394
395 return translate(VolumeManager::Instance()->addSandboxIds(appIds, sandboxIds));
396}
397
Jeff Sharkey401b2602017-12-14 22:15:20 -0700398binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
399 ENFORCE_UID(AID_SYSTEM);
400 ACQUIRE_LOCK;
401
402 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
403}
404
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600405binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700406 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600407 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600408 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600409 ACQUIRE_LOCK;
410
411 auto disk = VolumeManager::Instance()->findDisk(diskId);
412 if (disk == nullptr) {
413 return error("Failed to find disk " + diskId);
414 }
415 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700416 case PARTITION_TYPE_PUBLIC:
417 return translate(disk->partitionPublic());
418 case PARTITION_TYPE_PRIVATE:
419 return translate(disk->partitionPrivate());
420 case PARTITION_TYPE_MIXED:
421 return translate(disk->partitionMixed(ratio));
422 default:
423 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600424 }
425}
426
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600427binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700428 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600429 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600430 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600431 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600432 ACQUIRE_LOCK;
433
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600434 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600435}
436
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600437binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700438 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600439 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600440 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600441 ACQUIRE_LOCK;
442
443 auto vol = VolumeManager::Instance()->findVolume(volId);
444 if (vol == nullptr) {
445 return error("Failed to find volume " + volId);
446 }
447
448 vol->setMountFlags(mountFlags);
449 vol->setMountUserId(mountUserId);
450
451 int res = vol->mount();
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700452 if (res != OK) {
453 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600454 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700455 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
456 res = VolumeManager::Instance()->setPrimary(vol);
457 if (res != OK) {
458 return translate(res);
459 }
460 }
461 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600462}
463
464binder::Status VoldNativeService::unmount(const std::string& volId) {
465 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600466 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600467 ACQUIRE_LOCK;
468
469 auto vol = VolumeManager::Instance()->findVolume(volId);
470 if (vol == nullptr) {
471 return error("Failed to find volume " + volId);
472 }
473 return translate(vol->unmount());
474}
475
476binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
477 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600478 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600479 ACQUIRE_LOCK;
480
481 auto vol = VolumeManager::Instance()->findVolume(volId);
482 if (vol == nullptr) {
483 return error("Failed to find volume " + volId);
484 }
485 return translate(vol->format(fsType));
486}
487
Jeff Sharkey2048a282017-06-15 09:59:43 -0600488static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600489 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600490 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600491 } else {
492 auto vol = VolumeManager::Instance()->findVolume(volId);
493 if (vol == nullptr) {
494 return error("Failed to find volume " + volId);
495 }
496 if (vol->getType() != VolumeBase::Type::kPrivate) {
497 return error("Volume " + volId + " not private");
498 }
499 if (vol->getState() != VolumeBase::State::kMounted) {
500 return error("Volume " + volId + " not mounted");
501 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600502 *path = vol->getPath();
503 if (path->empty()) {
504 return error("Volume " + volId + " missing path");
505 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600506 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600507 return ok();
508}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600509
Jeff Sharkey2048a282017-06-15 09:59:43 -0600510binder::Status VoldNativeService::benchmark(
511 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
512 ENFORCE_UID(AID_SYSTEM);
513 CHECK_ARGUMENT_ID(volId);
514 ACQUIRE_LOCK;
515
516 std::string path;
517 auto status = pathForVolId(volId, &path);
518 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600519
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700520 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600521 return ok();
522}
523
Jeff Sharkey2048a282017-06-15 09:59:43 -0600524binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
525 ENFORCE_UID(AID_SYSTEM);
526 CHECK_ARGUMENT_ID(volId);
527 ACQUIRE_LOCK;
528
529 std::string path;
530 auto status = pathForVolId(volId, &path);
531 if (!status.isOk()) return status;
532 return translate(android::vold::CheckEncryption(path));
533}
534
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700535binder::Status VoldNativeService::moveStorage(
536 const std::string& fromVolId, const std::string& toVolId,
537 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600538 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600539 CHECK_ARGUMENT_ID(fromVolId);
540 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600541 ACQUIRE_LOCK;
542
543 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
544 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
545 if (fromVol == nullptr) {
546 return error("Failed to find volume " + fromVolId);
547 } else if (toVol == nullptr) {
548 return error("Failed to find volume " + toVolId);
549 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600550
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700551 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600552 return ok();
553}
554
555binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
556 ENFORCE_UID(AID_SYSTEM);
557 ACQUIRE_LOCK;
558
559 std::string tmp;
560 switch (remountMode) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700561 case REMOUNT_MODE_NONE:
562 tmp = "none";
563 break;
564 case REMOUNT_MODE_DEFAULT:
565 tmp = "default";
566 break;
567 case REMOUNT_MODE_READ:
568 tmp = "read";
569 break;
570 case REMOUNT_MODE_WRITE:
571 tmp = "write";
572 break;
573 default:
574 return error("Unknown mode " + std::to_string(remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600575 }
576 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
577}
578
579binder::Status VoldNativeService::mkdirs(const std::string& path) {
580 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600581 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600582 ACQUIRE_LOCK;
583
Jeff Sharkey3472e522017-10-06 18:02:53 -0600584 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600585}
586
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600587binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700588 const std::string& sourceKey, int32_t ownerGid,
589 std::string* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600590 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600591 CHECK_ARGUMENT_PATH(sourcePath);
592 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600593 ACQUIRE_LOCK;
594
595 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700596 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600597}
598
599binder::Status VoldNativeService::destroyObb(const std::string& volId) {
600 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600601 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600602 ACQUIRE_LOCK;
603
604 return translate(VolumeManager::Instance()->destroyObb(volId));
605}
606
Risan8c9f3322018-10-29 08:52:56 +0900607binder::Status VoldNativeService::createStubVolume(
608 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
609 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
610 ENFORCE_UID(AID_SYSTEM);
611 CHECK_ARGUMENT_PATH(sourcePath);
612 CHECK_ARGUMENT_PATH(mountPath);
613 CHECK_ARGUMENT_HEX(fsUuid);
614 // Label limitation seems to be different between fs (including allowed characters), so checking
615 // is quite meaningless.
616 ACQUIRE_LOCK;
617
618 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
619 fsUuid, fsLabel, _aidl_return));
620}
621
622binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
623 ENFORCE_UID(AID_SYSTEM);
624 CHECK_ARGUMENT_ID(volId);
625 ACQUIRE_LOCK;
626
627 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
628}
629
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700630binder::Status VoldNativeService::fstrim(
631 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600632 ENFORCE_UID(AID_SYSTEM);
633 ACQUIRE_LOCK;
634
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700635 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600636 return ok();
637}
638
Jin Qiana370c142017-10-17 15:41:45 -0700639binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700640 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700641 ENFORCE_UID(AID_SYSTEM);
642 ACQUIRE_LOCK;
643
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700644 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700645 return ok();
646}
647
648binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700649 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700650 ENFORCE_UID(AID_SYSTEM);
651 ACQUIRE_LOCK;
652
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700653 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700654 return ok();
655}
656
Risan8f6198d2018-10-26 20:56:45 -0600657binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700658 android::base::unique_fd* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600659 ENFORCE_UID(AID_SYSTEM);
660 ACQUIRE_LOCK;
661
Risan8f6198d2018-10-26 20:56:45 -0600662 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600663}
664
Risan8f6198d2018-10-26 20:56:45 -0600665binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600666 ENFORCE_UID(AID_SYSTEM);
667 ACQUIRE_LOCK;
668
Risan8f6198d2018-10-26 20:56:45 -0600669 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
670}
671
672binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
673 int32_t flags,
674 android::base::unique_fd* _aidl_return) {
675 ENFORCE_UID(AID_SYSTEM);
676 ACQUIRE_LOCK;
677
678 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
679 if (fd == -1) {
680 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
681 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
682 " flags: " + std::to_string(flags));
683 }
684
685 *_aidl_return = android::base::unique_fd(fd);
686 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600687}
688
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600689binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
690 ENFORCE_UID(AID_SYSTEM);
691 ACQUIRE_CRYPT_LOCK;
692
693 return translate(cryptfs_check_passwd(password.c_str()));
694}
695
696binder::Status VoldNativeService::fdeRestart() {
697 ENFORCE_UID(AID_SYSTEM);
698 ACQUIRE_CRYPT_LOCK;
699
700 // Spawn as thread so init can issue commands back to vold without
701 // causing deadlock, usually as a result of prep_data_fs.
702 std::thread(&cryptfs_restart).detach();
703 return ok();
704}
705
706binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
707 ENFORCE_UID(AID_SYSTEM);
708 ACQUIRE_CRYPT_LOCK;
709
710 *_aidl_return = cryptfs_crypto_complete();
711 return ok();
712}
713
714static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700715 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600716 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
717
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600718 for (int tries = 0; tries < 2; ++tries) {
719 int rc;
720 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800721 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600722 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800723 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600724 }
725
726 if (rc == 0) {
727 return 0;
728 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600729 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600730 }
731 }
732
733 return -1;
734}
735
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700736binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
737 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600738 ENFORCE_UID(AID_SYSTEM);
739 ACQUIRE_CRYPT_LOCK;
740
Paul Crowley0fd26262018-01-30 09:48:19 -0800741 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700742 if (fscrypt_is_native()) {
743 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
744 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600745 }
Eric Biggersa701c452018-10-23 13:06:55 -0700746 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600747
748 // Spawn as thread so init can issue commands back to vold without
749 // causing deadlock, usually as a result of prep_data_fs.
750 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
751 return ok();
752}
753
754binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700755 const std::string& password) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600756 ENFORCE_UID(AID_SYSTEM);
757 ACQUIRE_CRYPT_LOCK;
758
759 return translate(cryptfs_changepw(passwordType, password.c_str()));
760}
761
762binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
763 ENFORCE_UID(AID_SYSTEM);
764 ACQUIRE_CRYPT_LOCK;
765
766 return translate(cryptfs_verify_passwd(password.c_str()));
767}
768
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700769binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600770 ENFORCE_UID(AID_SYSTEM);
771 ACQUIRE_CRYPT_LOCK;
772
773 char buf[PROPERTY_VALUE_MAX];
774 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
775 return error(StringPrintf("Failed to read field %s", key.c_str()));
776 } else {
777 *_aidl_return = buf;
778 return ok();
779 }
780}
781
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700782binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600783 ENFORCE_UID(AID_SYSTEM);
784 ACQUIRE_CRYPT_LOCK;
785
786 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
787}
788
789binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
790 ENFORCE_UID(AID_SYSTEM);
791 ACQUIRE_CRYPT_LOCK;
792
793 *_aidl_return = cryptfs_get_password_type();
794 return ok();
795}
796
797binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
798 ENFORCE_UID(AID_SYSTEM);
799 ACQUIRE_CRYPT_LOCK;
800
801 const char* res = cryptfs_get_password();
802 if (res != nullptr) {
803 *_aidl_return = res;
804 }
805 return ok();
806}
807
808binder::Status VoldNativeService::fdeClearPassword() {
809 ENFORCE_UID(AID_SYSTEM);
810 ACQUIRE_CRYPT_LOCK;
811
812 cryptfs_clear_password();
813 return ok();
814}
815
816binder::Status VoldNativeService::fbeEnable() {
817 ENFORCE_UID(AID_SYSTEM);
818 ACQUIRE_CRYPT_LOCK;
819
Eric Biggersa701c452018-10-23 13:06:55 -0700820 return translateBool(fscrypt_initialize_global_de());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600821}
822
823binder::Status VoldNativeService::mountDefaultEncrypted() {
824 ENFORCE_UID(AID_SYSTEM);
825 ACQUIRE_CRYPT_LOCK;
826
Eric Biggersa701c452018-10-23 13:06:55 -0700827 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600828 // Spawn as thread so init can issue commands back to vold without
829 // causing deadlock, usually as a result of prep_data_fs.
830 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600831 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800832 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600833}
834
835binder::Status VoldNativeService::initUser0() {
836 ENFORCE_UID(AID_SYSTEM);
837 ACQUIRE_CRYPT_LOCK;
838
Eric Biggersa701c452018-10-23 13:06:55 -0700839 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600840}
841
842binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
843 ENFORCE_UID(AID_SYSTEM);
844 ACQUIRE_CRYPT_LOCK;
845
846 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
847 return ok();
848}
849
Paul Crowley0fd26262018-01-30 09:48:19 -0800850binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
851 ENFORCE_UID(AID_SYSTEM);
852 ACQUIRE_LOCK;
853
Eric Biggersa701c452018-10-23 13:06:55 -0700854 return translateBool(fscrypt_mount_metadata_encrypted(mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800855}
856
857binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
858 ENFORCE_UID(AID_SYSTEM);
859 ACQUIRE_LOCK;
860
Eric Biggersa701c452018-10-23 13:06:55 -0700861 return translateBool(fscrypt_mount_metadata_encrypted(mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800862}
863
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700864binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600865 ENFORCE_UID(AID_SYSTEM);
866 ACQUIRE_CRYPT_LOCK;
867
Eric Biggersa701c452018-10-23 13:06:55 -0700868 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600869}
870
871binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
872 ENFORCE_UID(AID_SYSTEM);
873 ACQUIRE_CRYPT_LOCK;
874
Eric Biggersa701c452018-10-23 13:06:55 -0700875 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600876}
877
878binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700879 const std::string& token,
880 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600881 ENFORCE_UID(AID_SYSTEM);
882 ACQUIRE_CRYPT_LOCK;
883
Eric Biggersa701c452018-10-23 13:06:55 -0700884 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600885}
886
887binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
888 ENFORCE_UID(AID_SYSTEM);
889 ACQUIRE_CRYPT_LOCK;
890
Eric Biggersa701c452018-10-23 13:06:55 -0700891 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600892}
893
894binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700895 const std::string& token,
896 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600897 ENFORCE_UID(AID_SYSTEM);
898 ACQUIRE_CRYPT_LOCK;
899
Eric Biggersa701c452018-10-23 13:06:55 -0700900 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600901}
902
903binder::Status VoldNativeService::lockUserKey(int32_t userId) {
904 ENFORCE_UID(AID_SYSTEM);
905 ACQUIRE_CRYPT_LOCK;
906
Eric Biggersa701c452018-10-23 13:06:55 -0700907 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600908}
909
910binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700911 int32_t userId, int32_t userSerial,
912 int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600913 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700914 std::string empty_string = "";
915 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700916 CHECK_ARGUMENT_HEX(uuid_);
917
918 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700919 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600920}
921
922binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700923 int32_t userId, int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600924 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700925 std::string empty_string = "";
926 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700927 CHECK_ARGUMENT_HEX(uuid_);
928
929 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700930 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600931}
932
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700933binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
934 int32_t appId, const std::string& sandboxId,
935 int32_t userId) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700936 ENFORCE_UID(AID_SYSTEM);
937 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
938 CHECK_ARGUMENT_SANDBOX_ID(sandboxId);
939 ACQUIRE_LOCK;
940
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700941 return translate(
942 VolumeManager::Instance()->prepareSandboxForApp(packageName, appId, sandboxId, userId));
943}
944
945binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700946 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700947 int32_t userId) {
948 ENFORCE_UID(AID_SYSTEM);
949 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
950 CHECK_ARGUMENT_SANDBOX_ID(sandboxId);
951 ACQUIRE_LOCK;
952
953 return translate(
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700954 VolumeManager::Instance()->destroySandboxForApp(packageName, sandboxId, userId));
Sudheer Shankac7562092018-08-24 10:20:56 -0700955}
956
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700957binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700958 ENFORCE_UID(AID_SYSTEM);
959 ACQUIRE_LOCK;
960
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700961 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700962}
963
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700964binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
965 ENFORCE_UID(AID_SYSTEM);
966 ACQUIRE_LOCK;
967
968 *_aidl_return = cp_needsRollback();
969 return ok();
970}
971
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700972binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
973 ENFORCE_UID(AID_SYSTEM);
974 ACQUIRE_LOCK;
975
976 *_aidl_return = cp_needsCheckpoint();
977 return ok();
978}
979
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700980binder::Status VoldNativeService::commitChanges() {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700981 ENFORCE_UID(AID_SYSTEM);
982 ACQUIRE_LOCK;
983
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700984 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700985}
986
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700987binder::Status VoldNativeService::prepareCheckpoint() {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700988 ENFORCE_UID(AID_SYSTEM);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700989 ACQUIRE_LOCK;
990
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700991 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700992}
993
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700994binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700995 ENFORCE_UID(AID_SYSTEM);
996 CHECK_ARGUMENT_PATH(mountPoint);
997 ACQUIRE_LOCK;
998
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700999 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -07001000}
1001
Daniel Rosenberge334aab2018-10-10 18:52:04 -07001002binder::Status VoldNativeService::markBootAttempt() {
Daniel Rosenberg4f684712018-08-28 01:58:49 -07001003 ENFORCE_UID(AID_SYSTEM);
1004 ACQUIRE_LOCK;
1005
Daniel Rosenberge334aab2018-10-10 18:52:04 -07001006 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -07001007}
1008
1009binder::Status VoldNativeService::abortChanges() {
1010 ENFORCE_UID(AID_SYSTEM);
1011 ACQUIRE_LOCK;
1012
Daniel Rosenberge334aab2018-10-10 18:52:04 -07001013 return cp_abortChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -07001014}
1015
Jeff Sharkey068c6be2017-09-06 13:47:40 -06001016} // namespace vold
1017} // namespace android