blob: 31890d62acc85d35908185072a2048578f914185 [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
Sudheer Shanka817b9112018-12-13 17:40:28 -0800559 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600560}
561
562binder::Status VoldNativeService::mkdirs(const std::string& path) {
563 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600564 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600565 ACQUIRE_LOCK;
566
Jeff Sharkey3472e522017-10-06 18:02:53 -0600567 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600568}
569
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600570binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700571 const std::string& sourceKey, int32_t ownerGid,
572 std::string* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600573 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600574 CHECK_ARGUMENT_PATH(sourcePath);
575 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600576 ACQUIRE_LOCK;
577
578 return translate(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700579 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600580}
581
582binder::Status VoldNativeService::destroyObb(const std::string& volId) {
583 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600584 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600585 ACQUIRE_LOCK;
586
587 return translate(VolumeManager::Instance()->destroyObb(volId));
588}
589
Risan8c9f3322018-10-29 08:52:56 +0900590binder::Status VoldNativeService::createStubVolume(
591 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
592 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
593 ENFORCE_UID(AID_SYSTEM);
594 CHECK_ARGUMENT_PATH(sourcePath);
595 CHECK_ARGUMENT_PATH(mountPath);
596 CHECK_ARGUMENT_HEX(fsUuid);
597 // Label limitation seems to be different between fs (including allowed characters), so checking
598 // is quite meaningless.
599 ACQUIRE_LOCK;
600
601 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
602 fsUuid, fsLabel, _aidl_return));
603}
604
605binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
606 ENFORCE_UID(AID_SYSTEM);
607 CHECK_ARGUMENT_ID(volId);
608 ACQUIRE_LOCK;
609
610 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
611}
612
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700613binder::Status VoldNativeService::fstrim(
614 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600615 ENFORCE_UID(AID_SYSTEM);
616 ACQUIRE_LOCK;
617
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700618 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600619 return ok();
620}
621
Jin Qiana370c142017-10-17 15:41:45 -0700622binder::Status VoldNativeService::runIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700623 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700624 ENFORCE_UID(AID_SYSTEM);
625 ACQUIRE_LOCK;
626
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700627 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700628 return ok();
629}
630
631binder::Status VoldNativeService::abortIdleMaint(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700632 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700633 ENFORCE_UID(AID_SYSTEM);
634 ACQUIRE_LOCK;
635
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700636 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700637 return ok();
638}
639
Risan8f6198d2018-10-26 20:56:45 -0600640binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700641 android::base::unique_fd* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600642 ENFORCE_UID(AID_SYSTEM);
643 ACQUIRE_LOCK;
644
Risan8f6198d2018-10-26 20:56:45 -0600645 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600646}
647
Risan8f6198d2018-10-26 20:56:45 -0600648binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600649 ENFORCE_UID(AID_SYSTEM);
650 ACQUIRE_LOCK;
651
Risan8f6198d2018-10-26 20:56:45 -0600652 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
653}
654
655binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
656 int32_t flags,
657 android::base::unique_fd* _aidl_return) {
658 ENFORCE_UID(AID_SYSTEM);
659 ACQUIRE_LOCK;
660
661 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
662 if (fd == -1) {
663 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
664 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
665 " flags: " + std::to_string(flags));
666 }
667
668 *_aidl_return = android::base::unique_fd(fd);
669 return ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600670}
671
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600672binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
673 ENFORCE_UID(AID_SYSTEM);
674 ACQUIRE_CRYPT_LOCK;
675
676 return translate(cryptfs_check_passwd(password.c_str()));
677}
678
679binder::Status VoldNativeService::fdeRestart() {
680 ENFORCE_UID(AID_SYSTEM);
681 ACQUIRE_CRYPT_LOCK;
682
683 // Spawn as thread so init can issue commands back to vold without
684 // causing deadlock, usually as a result of prep_data_fs.
685 std::thread(&cryptfs_restart).detach();
686 return ok();
687}
688
689binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
690 ENFORCE_UID(AID_SYSTEM);
691 ACQUIRE_CRYPT_LOCK;
692
693 *_aidl_return = cryptfs_crypto_complete();
694 return ok();
695}
696
697static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700698 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600699 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
700
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600701 for (int tries = 0; tries < 2; ++tries) {
702 int rc;
703 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800704 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800706 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600707 }
708
709 if (rc == 0) {
710 return 0;
711 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600712 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600713 }
714 }
715
716 return -1;
717}
718
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700719binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
720 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600721 ENFORCE_UID(AID_SYSTEM);
722 ACQUIRE_CRYPT_LOCK;
723
Paul Crowley0fd26262018-01-30 09:48:19 -0800724 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700725 if (fscrypt_is_native()) {
726 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
727 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600728 }
Eric Biggersa701c452018-10-23 13:06:55 -0700729 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600730
731 // Spawn as thread so init can issue commands back to vold without
732 // causing deadlock, usually as a result of prep_data_fs.
733 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
734 return ok();
735}
736
737binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700738 const std::string& password) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600739 ENFORCE_UID(AID_SYSTEM);
740 ACQUIRE_CRYPT_LOCK;
741
742 return translate(cryptfs_changepw(passwordType, password.c_str()));
743}
744
745binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
746 ENFORCE_UID(AID_SYSTEM);
747 ACQUIRE_CRYPT_LOCK;
748
749 return translate(cryptfs_verify_passwd(password.c_str()));
750}
751
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700752binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600753 ENFORCE_UID(AID_SYSTEM);
754 ACQUIRE_CRYPT_LOCK;
755
756 char buf[PROPERTY_VALUE_MAX];
757 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
758 return error(StringPrintf("Failed to read field %s", key.c_str()));
759 } else {
760 *_aidl_return = buf;
761 return ok();
762 }
763}
764
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700765binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600766 ENFORCE_UID(AID_SYSTEM);
767 ACQUIRE_CRYPT_LOCK;
768
769 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
770}
771
772binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
773 ENFORCE_UID(AID_SYSTEM);
774 ACQUIRE_CRYPT_LOCK;
775
776 *_aidl_return = cryptfs_get_password_type();
777 return ok();
778}
779
780binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
781 ENFORCE_UID(AID_SYSTEM);
782 ACQUIRE_CRYPT_LOCK;
783
784 const char* res = cryptfs_get_password();
785 if (res != nullptr) {
786 *_aidl_return = res;
787 }
788 return ok();
789}
790
791binder::Status VoldNativeService::fdeClearPassword() {
792 ENFORCE_UID(AID_SYSTEM);
793 ACQUIRE_CRYPT_LOCK;
794
795 cryptfs_clear_password();
796 return ok();
797}
798
799binder::Status VoldNativeService::fbeEnable() {
800 ENFORCE_UID(AID_SYSTEM);
801 ACQUIRE_CRYPT_LOCK;
802
Eric Biggersa701c452018-10-23 13:06:55 -0700803 return translateBool(fscrypt_initialize_global_de());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600804}
805
806binder::Status VoldNativeService::mountDefaultEncrypted() {
807 ENFORCE_UID(AID_SYSTEM);
808 ACQUIRE_CRYPT_LOCK;
809
Eric Biggersa701c452018-10-23 13:06:55 -0700810 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600811 // Spawn as thread so init can issue commands back to vold without
812 // causing deadlock, usually as a result of prep_data_fs.
813 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600814 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800815 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600816}
817
818binder::Status VoldNativeService::initUser0() {
819 ENFORCE_UID(AID_SYSTEM);
820 ACQUIRE_CRYPT_LOCK;
821
Eric Biggersa701c452018-10-23 13:06:55 -0700822 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600823}
824
825binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
826 ENFORCE_UID(AID_SYSTEM);
827 ACQUIRE_CRYPT_LOCK;
828
829 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
830 return ok();
831}
832
Paul Crowley0fd26262018-01-30 09:48:19 -0800833binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
834 ENFORCE_UID(AID_SYSTEM);
835 ACQUIRE_LOCK;
836
Eric Biggersa701c452018-10-23 13:06:55 -0700837 return translateBool(fscrypt_mount_metadata_encrypted(mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800838}
839
840binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
841 ENFORCE_UID(AID_SYSTEM);
842 ACQUIRE_LOCK;
843
Eric Biggersa701c452018-10-23 13:06:55 -0700844 return translateBool(fscrypt_mount_metadata_encrypted(mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800845}
846
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700847binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600848 ENFORCE_UID(AID_SYSTEM);
849 ACQUIRE_CRYPT_LOCK;
850
Eric Biggersa701c452018-10-23 13:06:55 -0700851 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600852}
853
854binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
855 ENFORCE_UID(AID_SYSTEM);
856 ACQUIRE_CRYPT_LOCK;
857
Eric Biggersa701c452018-10-23 13:06:55 -0700858 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600859}
860
861binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700862 const std::string& token,
863 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600864 ENFORCE_UID(AID_SYSTEM);
865 ACQUIRE_CRYPT_LOCK;
866
Eric Biggersa701c452018-10-23 13:06:55 -0700867 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600868}
869
870binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
871 ENFORCE_UID(AID_SYSTEM);
872 ACQUIRE_CRYPT_LOCK;
873
Eric Biggersa701c452018-10-23 13:06:55 -0700874 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600875}
876
877binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700878 const std::string& token,
879 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600880 ENFORCE_UID(AID_SYSTEM);
881 ACQUIRE_CRYPT_LOCK;
882
Eric Biggersa701c452018-10-23 13:06:55 -0700883 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600884}
885
886binder::Status VoldNativeService::lockUserKey(int32_t userId) {
887 ENFORCE_UID(AID_SYSTEM);
888 ACQUIRE_CRYPT_LOCK;
889
Eric Biggersa701c452018-10-23 13:06:55 -0700890 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600891}
892
893binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700894 int32_t userId, int32_t userSerial,
895 int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600896 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700897 std::string empty_string = "";
898 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700899 CHECK_ARGUMENT_HEX(uuid_);
900
901 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700902 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600903}
904
905binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700906 int32_t userId, int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600907 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700908 std::string empty_string = "";
909 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700910 CHECK_ARGUMENT_HEX(uuid_);
911
912 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700913 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600914}
915
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700916binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
917 int32_t appId, const std::string& sandboxId,
918 int32_t userId) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700919 ENFORCE_UID(AID_SYSTEM);
920 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
921 CHECK_ARGUMENT_SANDBOX_ID(sandboxId);
922 ACQUIRE_LOCK;
923
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700924 return translate(
925 VolumeManager::Instance()->prepareSandboxForApp(packageName, appId, sandboxId, userId));
926}
927
928binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700929 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700930 int32_t userId) {
931 ENFORCE_UID(AID_SYSTEM);
932 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
933 CHECK_ARGUMENT_SANDBOX_ID(sandboxId);
934 ACQUIRE_LOCK;
935
936 return translate(
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700937 VolumeManager::Instance()->destroySandboxForApp(packageName, sandboxId, userId));
Sudheer Shankac7562092018-08-24 10:20:56 -0700938}
939
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700940binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700941 ENFORCE_UID(AID_SYSTEM);
942 ACQUIRE_LOCK;
943
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700944 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700945}
946
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700947binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
948 ENFORCE_UID(AID_SYSTEM);
949 ACQUIRE_LOCK;
950
951 *_aidl_return = cp_needsRollback();
952 return ok();
953}
954
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700955binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
956 ENFORCE_UID(AID_SYSTEM);
957 ACQUIRE_LOCK;
958
959 *_aidl_return = cp_needsCheckpoint();
960 return ok();
961}
962
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700963binder::Status VoldNativeService::commitChanges() {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700964 ENFORCE_UID(AID_SYSTEM);
965 ACQUIRE_LOCK;
966
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700967 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700968}
969
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700970binder::Status VoldNativeService::prepareCheckpoint() {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700971 ENFORCE_UID(AID_SYSTEM);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700972 ACQUIRE_LOCK;
973
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700974 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700975}
976
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700977binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700978 ENFORCE_UID(AID_SYSTEM);
979 CHECK_ARGUMENT_PATH(mountPoint);
980 ACQUIRE_LOCK;
981
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700982 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700983}
984
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700985binder::Status VoldNativeService::markBootAttempt() {
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700986 ENFORCE_UID(AID_SYSTEM);
987 ACQUIRE_LOCK;
988
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700989 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700990}
991
992binder::Status VoldNativeService::abortChanges() {
993 ENFORCE_UID(AID_SYSTEM);
994 ACQUIRE_LOCK;
995
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700996 return cp_abortChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700997}
998
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600999} // namespace vold
1000} // namespace android