blob: a06b357098cb7283920980a398d5606697caf886 [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 Rosenberg65f99c92018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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
Paul Crowley8915d622018-09-18 15:14:18 -0700150#define ENFORCE_UID(uid) \
151 { \
152 binder::Status status = checkUid((uid)); \
153 if (!status.isOk()) { \
154 return status; \
155 } \
156 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600157
Paul Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-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 Crowley8915d622018-09-18 15:14:18 -0700219 const android::sp<android::os::IVoldListener>& listener) {
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600220 ENFORCE_UID(AID_SYSTEM);
221 ACQUIRE_LOCK;
222
223 VolumeManager::Instance()->setListener(listener);
224 return ok();
225}
226
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600227binder::Status VoldNativeService::monitor() {
228 ENFORCE_UID(AID_SYSTEM);
229
230 // Simply acquire/release each lock for watchdog
Paul Crowley8915d622018-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() {
238 ENFORCE_UID(AID_SYSTEM);
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() {
245 ENFORCE_UID(AID_SYSTEM);
246 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) {
252 ENFORCE_UID(AID_SYSTEM);
253 ACQUIRE_LOCK;
254
255 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
256}
257
258binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
259 ENFORCE_UID(AID_SYSTEM);
260 ACQUIRE_LOCK;
261
262 return translate(VolumeManager::Instance()->onUserRemoved(userId));
263}
264
265binder::Status VoldNativeService::onUserStarted(int32_t userId) {
266 ENFORCE_UID(AID_SYSTEM);
267 ACQUIRE_LOCK;
268
269 return translate(VolumeManager::Instance()->onUserStarted(userId));
270}
271
272binder::Status VoldNativeService::onUserStopped(int32_t userId) {
273 ENFORCE_UID(AID_SYSTEM);
274 ACQUIRE_LOCK;
275
276 return translate(VolumeManager::Instance()->onUserStopped(userId));
277}
278
Jeff Sharkey401b2602017-12-14 22:15:20 -0700279binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
280 ENFORCE_UID(AID_SYSTEM);
281 ACQUIRE_LOCK;
282
283 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
284}
285
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600286binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowley8915d622018-09-18 15:14:18 -0700287 int32_t ratio) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600288 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600289 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600290 ACQUIRE_LOCK;
291
292 auto disk = VolumeManager::Instance()->findDisk(diskId);
293 if (disk == nullptr) {
294 return error("Failed to find disk " + diskId);
295 }
296 switch (partitionType) {
Paul Crowley8915d622018-09-18 15:14:18 -0700297 case PARTITION_TYPE_PUBLIC:
298 return translate(disk->partitionPublic());
299 case PARTITION_TYPE_PRIVATE:
300 return translate(disk->partitionPrivate());
301 case PARTITION_TYPE_MIXED:
302 return translate(disk->partitionMixed(ratio));
303 default:
304 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600305 }
306}
307
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600308binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowley8915d622018-09-18 15:14:18 -0700309 const std::string& fsUuid) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600310 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600311 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600312 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600313 ACQUIRE_LOCK;
314
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600315 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600316}
317
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600318binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags,
Paul Crowley8915d622018-09-18 15:14:18 -0700319 int32_t mountUserId) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600320 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600321 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600322 ACQUIRE_LOCK;
323
324 auto vol = VolumeManager::Instance()->findVolume(volId);
325 if (vol == nullptr) {
326 return error("Failed to find volume " + volId);
327 }
328
329 vol->setMountFlags(mountFlags);
330 vol->setMountUserId(mountUserId);
331
332 int res = vol->mount();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600333 if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600334 VolumeManager::Instance()->setPrimary(vol);
335 }
336 return translate(res);
337}
338
339binder::Status VoldNativeService::unmount(const std::string& volId) {
340 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600341 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600342 ACQUIRE_LOCK;
343
344 auto vol = VolumeManager::Instance()->findVolume(volId);
345 if (vol == nullptr) {
346 return error("Failed to find volume " + volId);
347 }
348 return translate(vol->unmount());
349}
350
351binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
352 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600353 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600354 ACQUIRE_LOCK;
355
356 auto vol = VolumeManager::Instance()->findVolume(volId);
357 if (vol == nullptr) {
358 return error("Failed to find volume " + volId);
359 }
360 return translate(vol->format(fsType));
361}
362
Jeff Sharkey2048a282017-06-15 09:59:43 -0600363static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600364 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600365 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600366 } else {
367 auto vol = VolumeManager::Instance()->findVolume(volId);
368 if (vol == nullptr) {
369 return error("Failed to find volume " + volId);
370 }
371 if (vol->getType() != VolumeBase::Type::kPrivate) {
372 return error("Volume " + volId + " not private");
373 }
374 if (vol->getState() != VolumeBase::State::kMounted) {
375 return error("Volume " + volId + " not mounted");
376 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600377 *path = vol->getPath();
378 if (path->empty()) {
379 return error("Volume " + volId + " missing path");
380 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600381 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600382 return ok();
383}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600384
Jeff Sharkey2048a282017-06-15 09:59:43 -0600385binder::Status VoldNativeService::benchmark(
386 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
387 ENFORCE_UID(AID_SYSTEM);
388 CHECK_ARGUMENT_ID(volId);
389 ACQUIRE_LOCK;
390
391 std::string path;
392 auto status = pathForVolId(volId, &path);
393 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600394
Paul Crowley8915d622018-09-18 15:14:18 -0700395 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600396 return ok();
397}
398
Jeff Sharkey2048a282017-06-15 09:59:43 -0600399binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
400 ENFORCE_UID(AID_SYSTEM);
401 CHECK_ARGUMENT_ID(volId);
402 ACQUIRE_LOCK;
403
404 std::string path;
405 auto status = pathForVolId(volId, &path);
406 if (!status.isOk()) return status;
407 return translate(android::vold::CheckEncryption(path));
408}
409
Paul Crowley8915d622018-09-18 15:14:18 -0700410binder::Status VoldNativeService::moveStorage(
411 const std::string& fromVolId, const std::string& toVolId,
412 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600413 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600414 CHECK_ARGUMENT_ID(fromVolId);
415 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600416 ACQUIRE_LOCK;
417
418 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
419 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
420 if (fromVol == nullptr) {
421 return error("Failed to find volume " + fromVolId);
422 } else if (toVol == nullptr) {
423 return error("Failed to find volume " + toVolId);
424 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600425
Paul Crowley8915d622018-09-18 15:14:18 -0700426 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600427 return ok();
428}
429
430binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
431 ENFORCE_UID(AID_SYSTEM);
432 ACQUIRE_LOCK;
433
434 std::string tmp;
435 switch (remountMode) {
Paul Crowley8915d622018-09-18 15:14:18 -0700436 case REMOUNT_MODE_NONE:
437 tmp = "none";
438 break;
439 case REMOUNT_MODE_DEFAULT:
440 tmp = "default";
441 break;
442 case REMOUNT_MODE_READ:
443 tmp = "read";
444 break;
445 case REMOUNT_MODE_WRITE:
446 tmp = "write";
447 break;
448 default:
449 return error("Unknown mode " + std::to_string(remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600450 }
451 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
452}
453
454binder::Status VoldNativeService::mkdirs(const std::string& path) {
455 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600456 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600457 ACQUIRE_LOCK;
458
Jeff Sharkey3472e522017-10-06 18:02:53 -0600459 return translate(VolumeManager::Instance()->mkdirs(path));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600460}
461
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600462binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowley8915d622018-09-18 15:14:18 -0700463 const std::string& sourceKey, int32_t ownerGid,
464 std::string* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600465 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600466 CHECK_ARGUMENT_PATH(sourcePath);
467 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600468 ACQUIRE_LOCK;
469
470 return translate(
Paul Crowley8915d622018-09-18 15:14:18 -0700471 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600472}
473
474binder::Status VoldNativeService::destroyObb(const std::string& volId) {
475 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600476 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600477 ACQUIRE_LOCK;
478
479 return translate(VolumeManager::Instance()->destroyObb(volId));
480}
481
Risan8c9f3322018-10-29 08:52:56 +0900482binder::Status VoldNativeService::createStubVolume(
483 const std::string& sourcePath, const std::string& mountPath, const std::string& fsType,
484 const std::string& fsUuid, const std::string& fsLabel, std::string* _aidl_return) {
485 ENFORCE_UID(AID_SYSTEM);
486 CHECK_ARGUMENT_PATH(sourcePath);
487 CHECK_ARGUMENT_PATH(mountPath);
488 CHECK_ARGUMENT_HEX(fsUuid);
489 // Label limitation seems to be different between fs (including allowed characters), so checking
490 // is quite meaningless.
491 ACQUIRE_LOCK;
492
493 return translate(VolumeManager::Instance()->createStubVolume(sourcePath, mountPath, fsType,
494 fsUuid, fsLabel, _aidl_return));
495}
496
497binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
498 ENFORCE_UID(AID_SYSTEM);
499 CHECK_ARGUMENT_ID(volId);
500 ACQUIRE_LOCK;
501
502 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
503}
504
Paul Crowley8915d622018-09-18 15:14:18 -0700505binder::Status VoldNativeService::fstrim(
506 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600507 ENFORCE_UID(AID_SYSTEM);
508 ACQUIRE_LOCK;
509
Paul Crowley8915d622018-09-18 15:14:18 -0700510 std::thread([=]() { android::vold::Trim(listener); }).detach();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600511 return ok();
512}
513
Jin Qiana370c142017-10-17 15:41:45 -0700514binder::Status VoldNativeService::runIdleMaint(
Paul Crowley8915d622018-09-18 15:14:18 -0700515 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700516 ENFORCE_UID(AID_SYSTEM);
517 ACQUIRE_LOCK;
518
Paul Crowley8915d622018-09-18 15:14:18 -0700519 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700520 return ok();
521}
522
523binder::Status VoldNativeService::abortIdleMaint(
Paul Crowley8915d622018-09-18 15:14:18 -0700524 const android::sp<android::os::IVoldTaskListener>& listener) {
Jin Qiana370c142017-10-17 15:41:45 -0700525 ENFORCE_UID(AID_SYSTEM);
526 ACQUIRE_LOCK;
527
Paul Crowley8915d622018-09-18 15:14:18 -0700528 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Jin Qiana370c142017-10-17 15:41:45 -0700529 return ok();
530}
531
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600532binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId,
Paul Crowley8915d622018-09-18 15:14:18 -0700533 android::base::unique_fd* _aidl_return) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600534 ENFORCE_UID(AID_SYSTEM);
535 ACQUIRE_LOCK;
536
537 return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return));
538}
539
540binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) {
541 ENFORCE_UID(AID_SYSTEM);
542 ACQUIRE_LOCK;
543
544 return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId));
545}
546
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600547binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
548 ENFORCE_UID(AID_SYSTEM);
549 ACQUIRE_CRYPT_LOCK;
550
551 return translate(cryptfs_check_passwd(password.c_str()));
552}
553
554binder::Status VoldNativeService::fdeRestart() {
555 ENFORCE_UID(AID_SYSTEM);
556 ACQUIRE_CRYPT_LOCK;
557
558 // Spawn as thread so init can issue commands back to vold without
559 // causing deadlock, usually as a result of prep_data_fs.
560 std::thread(&cryptfs_restart).detach();
561 return ok();
562}
563
564binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
565 ENFORCE_UID(AID_SYSTEM);
566 ACQUIRE_CRYPT_LOCK;
567
568 *_aidl_return = cryptfs_crypto_complete();
569 return ok();
570}
571
572static int fdeEnableInternal(int32_t passwordType, const std::string& password,
Paul Crowley8915d622018-09-18 15:14:18 -0700573 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600574 bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0;
575
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600576 for (int tries = 0; tries < 2; ++tries) {
577 int rc;
578 if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800579 rc = cryptfs_enable_default(noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600580 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -0800581 rc = cryptfs_enable(passwordType, password.c_str(), noUi);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600582 }
583
584 if (rc == 0) {
585 return 0;
586 } else if (tries == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600587 KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600588 }
589 }
590
591 return -1;
592}
593
Paul Crowley8915d622018-09-18 15:14:18 -0700594binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
595 int32_t encryptionFlags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600596 ENFORCE_UID(AID_SYSTEM);
597 ACQUIRE_CRYPT_LOCK;
598
Paul Crowley0fd26262018-01-30 09:48:19 -0800599 LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")";
Eric Biggersa701c452018-10-23 13:06:55 -0700600 if (fscrypt_is_native()) {
601 LOG(ERROR) << "fscrypt_is_native, fdeEnable invalid";
602 return error("fscrypt_is_native, fdeEnable invalid");
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600603 }
Eric Biggersa701c452018-10-23 13:06:55 -0700604 LOG(DEBUG) << "!fscrypt_is_native, spawning fdeEnableInternal";
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600605
606 // Spawn as thread so init can issue commands back to vold without
607 // causing deadlock, usually as a result of prep_data_fs.
608 std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
609 return ok();
610}
611
612binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowley8915d622018-09-18 15:14:18 -0700613 const std::string& password) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600614 ENFORCE_UID(AID_SYSTEM);
615 ACQUIRE_CRYPT_LOCK;
616
617 return translate(cryptfs_changepw(passwordType, password.c_str()));
618}
619
620binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
621 ENFORCE_UID(AID_SYSTEM);
622 ACQUIRE_CRYPT_LOCK;
623
624 return translate(cryptfs_verify_passwd(password.c_str()));
625}
626
Paul Crowley8915d622018-09-18 15:14:18 -0700627binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600628 ENFORCE_UID(AID_SYSTEM);
629 ACQUIRE_CRYPT_LOCK;
630
631 char buf[PROPERTY_VALUE_MAX];
632 if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) {
633 return error(StringPrintf("Failed to read field %s", key.c_str()));
634 } else {
635 *_aidl_return = buf;
636 return ok();
637 }
638}
639
Paul Crowley8915d622018-09-18 15:14:18 -0700640binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600641 ENFORCE_UID(AID_SYSTEM);
642 ACQUIRE_CRYPT_LOCK;
643
644 return translate(cryptfs_setfield(key.c_str(), value.c_str()));
645}
646
647binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
648 ENFORCE_UID(AID_SYSTEM);
649 ACQUIRE_CRYPT_LOCK;
650
651 *_aidl_return = cryptfs_get_password_type();
652 return ok();
653}
654
655binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
656 ENFORCE_UID(AID_SYSTEM);
657 ACQUIRE_CRYPT_LOCK;
658
659 const char* res = cryptfs_get_password();
660 if (res != nullptr) {
661 *_aidl_return = res;
662 }
663 return ok();
664}
665
666binder::Status VoldNativeService::fdeClearPassword() {
667 ENFORCE_UID(AID_SYSTEM);
668 ACQUIRE_CRYPT_LOCK;
669
670 cryptfs_clear_password();
671 return ok();
672}
673
674binder::Status VoldNativeService::fbeEnable() {
675 ENFORCE_UID(AID_SYSTEM);
676 ACQUIRE_CRYPT_LOCK;
677
Eric Biggersa701c452018-10-23 13:06:55 -0700678 return translateBool(fscrypt_initialize_global_de());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600679}
680
681binder::Status VoldNativeService::mountDefaultEncrypted() {
682 ENFORCE_UID(AID_SYSTEM);
683 ACQUIRE_CRYPT_LOCK;
684
Eric Biggersa701c452018-10-23 13:06:55 -0700685 if (!fscrypt_is_native()) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600686 // Spawn as thread so init can issue commands back to vold without
687 // causing deadlock, usually as a result of prep_data_fs.
688 std::thread(&cryptfs_mount_default_encrypted).detach();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600689 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800690 return ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600691}
692
693binder::Status VoldNativeService::initUser0() {
694 ENFORCE_UID(AID_SYSTEM);
695 ACQUIRE_CRYPT_LOCK;
696
Eric Biggersa701c452018-10-23 13:06:55 -0700697 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600698}
699
700binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
701 ENFORCE_UID(AID_SYSTEM);
702 ACQUIRE_CRYPT_LOCK;
703
704 *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
705 return ok();
706}
707
Paul Crowley0fd26262018-01-30 09:48:19 -0800708binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) {
709 ENFORCE_UID(AID_SYSTEM);
710 ACQUIRE_LOCK;
711
Eric Biggersa701c452018-10-23 13:06:55 -0700712 return translateBool(fscrypt_mount_metadata_encrypted(mountPoint, false));
Paul Crowley0fd26262018-01-30 09:48:19 -0800713}
714
715binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) {
716 ENFORCE_UID(AID_SYSTEM);
717 ACQUIRE_LOCK;
718
Eric Biggersa701c452018-10-23 13:06:55 -0700719 return translateBool(fscrypt_mount_metadata_encrypted(mountPoint, true));
Paul Crowley0fd26262018-01-30 09:48:19 -0800720}
721
Paul Crowley8915d622018-09-18 15:14:18 -0700722binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600723 ENFORCE_UID(AID_SYSTEM);
724 ACQUIRE_CRYPT_LOCK;
725
Eric Biggersa701c452018-10-23 13:06:55 -0700726 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600727}
728
729binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
730 ENFORCE_UID(AID_SYSTEM);
731 ACQUIRE_CRYPT_LOCK;
732
Eric Biggersa701c452018-10-23 13:06:55 -0700733 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600734}
735
736binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowley8915d622018-09-18 15:14:18 -0700737 const std::string& token,
738 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600739 ENFORCE_UID(AID_SYSTEM);
740 ACQUIRE_CRYPT_LOCK;
741
Eric Biggersa701c452018-10-23 13:06:55 -0700742 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600743}
744
745binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
746 ENFORCE_UID(AID_SYSTEM);
747 ACQUIRE_CRYPT_LOCK;
748
Eric Biggersa701c452018-10-23 13:06:55 -0700749 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600750}
751
752binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowley8915d622018-09-18 15:14:18 -0700753 const std::string& token,
754 const std::string& secret) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600755 ENFORCE_UID(AID_SYSTEM);
756 ACQUIRE_CRYPT_LOCK;
757
Eric Biggersa701c452018-10-23 13:06:55 -0700758 return translateBool(fscrypt_unlock_user_key(userId, userSerial, token, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600759}
760
761binder::Status VoldNativeService::lockUserKey(int32_t userId) {
762 ENFORCE_UID(AID_SYSTEM);
763 ACQUIRE_CRYPT_LOCK;
764
Eric Biggersa701c452018-10-23 13:06:55 -0700765 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600766}
767
768binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowley8915d622018-09-18 15:14:18 -0700769 int32_t userId, int32_t userSerial,
770 int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600771 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700772 std::string empty_string = "";
773 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700774 CHECK_ARGUMENT_HEX(uuid_);
775
776 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700777 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600778}
779
780binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid,
Paul Crowley8915d622018-09-18 15:14:18 -0700781 int32_t userId, int32_t flags) {
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600782 ENFORCE_UID(AID_SYSTEM);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700783 std::string empty_string = "";
784 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700785 CHECK_ARGUMENT_HEX(uuid_);
786
787 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700788 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600789}
790
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700791binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700792 ENFORCE_UID(AID_SYSTEM);
793 ACQUIRE_LOCK;
794
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700795 return cp_startCheckpoint(retry);
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700796}
797
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700798binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
799 ENFORCE_UID(AID_SYSTEM);
800 ACQUIRE_LOCK;
801
802 *_aidl_return = cp_needsRollback();
803 return ok();
804}
805
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700806binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
807 ENFORCE_UID(AID_SYSTEM);
808 ACQUIRE_LOCK;
809
810 *_aidl_return = cp_needsCheckpoint();
811 return ok();
812}
813
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700814binder::Status VoldNativeService::commitChanges() {
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700815 ENFORCE_UID(AID_SYSTEM);
816 ACQUIRE_LOCK;
817
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700818 return cp_commitChanges();
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700819}
820
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700821binder::Status VoldNativeService::prepareCheckpoint() {
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700822 ENFORCE_UID(AID_SYSTEM);
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700823 ACQUIRE_LOCK;
824
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700825 return cp_prepareCheckpoint();
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700826}
827
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700828binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700829 ENFORCE_UID(AID_SYSTEM);
830 CHECK_ARGUMENT_PATH(mountPoint);
831 ACQUIRE_LOCK;
832
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700833 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700834}
835
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700836binder::Status VoldNativeService::markBootAttempt() {
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700837 ENFORCE_UID(AID_SYSTEM);
838 ACQUIRE_LOCK;
839
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700840 return cp_markBootAttempt();
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700841}
842
843binder::Status VoldNativeService::abortChanges() {
844 ENFORCE_UID(AID_SYSTEM);
845 ACQUIRE_LOCK;
846
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700847 return cp_abortChanges();
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700848}
849
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600850} // namespace vold
851} // namespace android